https://kotlinlang.org logo
#getting-started
Title
# getting-started
n

Nat Strangerweather

12/23/2020, 9:26 AM
Hi, I need to use this format
32 + 64 + 4 + 128
. The problem is that I don't want the numbers to be added up. If I use quotation marks to turn it into a String, I get an error because the method needs Ints. Any ideas?
⁉️ 3
v

Vampire

12/23/2020, 9:33 AM
What is the signature of the method you try to call?
d

Dominik wuttke

12/23/2020, 9:34 AM
How about "$value1 + $value2 + $value3" simply make it a String without any operations
v

Vampire

12/23/2020, 9:35 AM
He said making it a
String
does not work
n

Nat Strangerweather

12/23/2020, 9:35 AM
but then it says Ints are needed. Here is the method:
Copy code
notificationManager.setNotificationPolicy(
                        NotificationManager.Policy(
                            policyCodeA + policyCodeB + policyCodeC + policyCodeD + policyCodeE,
                            0,
                            0
                        )
                    )
v

Vampire

12/23/2020, 9:38 AM
And what do you want if not the numbers added up?
d

Dominik wuttke

12/23/2020, 9:38 AM
looked up the documentation at https://developer.android.com/reference/android/app/NotificationManager.Policy You need to create a bitmask from the flags you want to set
1
you can't simply add any Int of a policy you want to set
v

Vampire

12/23/2020, 9:39 AM
Why not? Setting bitmask is the same as adding up the single values
n

Nat Strangerweather

12/23/2020, 9:40 AM
Ok, it seemed to work in Java. I am remaking my app in Kotlin and it's not working..
v

Vampire

12/23/2020, 9:40 AM
Define "it's not working"
😄 1
n

Nat Strangerweather

12/23/2020, 9:40 AM
It adds up the codes
In Jva it simply used the codes
like the 4 codes
v

Vampire

12/23/2020, 9:41 AM
So does Java and that's perfectly fine
What don't you like with the codes being summed?
d

Dominik wuttke

12/23/2020, 9:41 AM
But as @Vampire said, this should still work
n

Nat Strangerweather

12/23/2020, 9:41 AM
Ok, never mind, I know what you mean. I need to reexamine my code in Java properly. Thanks in any case!
v

Vampire

12/23/2020, 9:43 AM
I mean, if you really don't want to sum, you can use bitwise or instead which is semantically more correct
policyCodeA or policyCodeB or policyCodeC or policyCodeD or policyCodeE
Like in Java
policyCodeA | policyCodeB | policyCodeC | policyCodeD | policyCodeE
But the result will be the same
n

Nat Strangerweather

12/23/2020, 9:44 AM
To answer your question about not adding the codes, it's because then it creates another code which is for another setting...
Ok, thanks I'll try that
v

Vampire

12/23/2020, 9:45 AM
No it is not, the codes are all numbers where one bit is set and each is different
This way you can combine them in a bitmask
Either by bitwise OR operation or by summing them up
There are no two values you can sum up to get another ones value
👍 1
n

Nat Strangerweather

12/23/2020, 9:48 AM
Yes OR seems to work. But that's interesting about summing up. I'll try again!
v

Vampire

12/23/2020, 9:49 AM
As I said, OR is semantically more correct, but the result will be the same. Unless there are pre-combined constants where already multiple bits are set
But then it would have failed in Java also
👍 1
Indeed there are such combined constants
For example
CONVERSATION_SENDERS_ANYONE
is
1
(
00000001
),
CONVERSATION_SENDERS_IMPORTANT
is
2
(
00000010
),
CONVERSATION_SENDERS_NONE
is
3
(
00000011
). So combining anyone and important results in none. And if you properly use
or
to combine anyone and none, you still end up with none. But if you use
-
to combine anyone and none you suddenly have
4
which is
PRIORITY_CATEGORY_MESSAGES
n

Nat Strangerweather

12/23/2020, 10:04 AM
Works great, thanks again!
3 Views