Hi, I need to use this format `32 + 64 + 4 + 128` ...
# getting-started
n
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
What is the signature of the method you try to call?
d
How about "$value1 + $value2 + $value3" simply make it a String without any operations
v
He said making it a
String
does not work
n
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
And what do you want if not the numbers added up?
d
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
Why not? Setting bitmask is the same as adding up the single values
n
Ok, it seemed to work in Java. I am remaking my app in Kotlin and it's not working..
v
Define "it's not working"
😄 1
n
It adds up the codes
In Jva it simply used the codes
like the 4 codes
v
So does Java and that's perfectly fine
What don't you like with the codes being summed?
d
But as @Vampire said, this should still work
n
Ok, never mind, I know what you mean. I need to reexamine my code in Java properly. Thanks in any case!
v
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
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
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
Yes OR seems to work. But that's interesting about summing up. I'll try again!
v
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
Works great, thanks again!