How long can be the value of "id" in notificationManager.notify() - android

I am trying to pass unique value to notificationManager.notify(). The shortest way that's coming into my mind is System.currentTimeMillis().
So i did following to get unique value.
Long getCurrentTime= System.currentTimeMillis();
int id= Math.abs(getCurrentTime.intValue());
notificationManager.notify("com.myapp.app",id , notification);
System.currentTimeMillis() returns 13 digit long value.
I am little afraid about the length of this generated id.
I want to know that what is recommanded length of id in notificationManager.notify() method.

Why don't you just create a static counter in your main activity and you increment it before call notify
Main Activity
public static int NOTIFY_COUNTER_ID= 1;
Notification class
notificationManager.notify("com.myapp.app",++MainActivity.NOTIFY_COUNTER_ID, notification);
EDIT
As you mentioned in the comment, the app might be killed so you won't be able to access the static resource.
In order to avoid that you can use Shared preferences to save your incremental ID.
Or IMHO if you use Math.rand() with a big "gap" you should be fine.

System.currentTimeMillis() is too long to manage identifier.
Id is int, its 32 bit in java,
Recommended value will be -2,147,483,648 to +2,147,483,647

Related

Firebase CompareTo Method

i am trying to use compareTo method based on firebase docs to get how long ago was the database entry created.
https://firebase.google.com/docs/reference/android/com/google/firebase/Timestamp
I am trying to do something like this
var postedAgo = myEntry.dateCreated.compareTo(firebase.database.ServerValue.TIMESTAMP);
myEntry.dateCreated is stored TIMESTAMP, so all i got in my db is numerical value. I hope thats right. But the problem is when i log this it says that compareTo is not a function.
I am obviously doing something wrong, but i cant find almost anything on compareTo beyond the documentation. I wonder if anyone is even using it.
Thanks
Luke
The Firebase Database ServerValue.TIMESTAMP is not an actual value of a timestamp, but a so-called marker value that the server recognized (and then replaces by the current timestamp when writing to the database). Because of this, you cannot use ServerValue.TIMESTAMP in pure client-side operations.
If you want to determine how long ago a node was created, you'd take the timestamp from that node and subtract it from the current timestamp. So if myEntry.dateCreated is a timestamp that was written with ServerValue.TIMESTAMP, you could do:
System.currentTimeMillis() - myEntry.dateCreated

Timestamp as unique ID

Is there something wrong if i use
Long uniqueId = System.currentTimeMillis()/1000;
String documentName = ""+ uniqueId;
as for example a document name?
Unless the user has a wrong Date it will be always unique right?
Or is there a better way to create always unique number values?
The reason i use Long uniqueId = System.currentTimeMillis()/1000;
is because it has to be unique even if the user generates that document from another device without the same data which means that i cannot create and save unique values and simply add +1 to it.
If you use
System.currentTimeMillis()/1000
then if you save 2 files in the same second you will have 2 identical names for 2 different files.
So i suggest you to use
UUID.randomUUID().toString()

Android - frequent updates with Strings

I have a Service that sends out updates every few hundred milliseconds. I have a String that gets created each time with a description of the event ("Time elapsed is 32 seconds"). Unfortunately I can't just use ints because the content can change depending on the event (usually however it's the same event type) and the feedback is going back to the user. Is there a way I can statically reuse the same String so that there aren't 100s of String allocations per minute? Even if I reuse the same variable, ie:
mEventUpdate = "Time elapsed is " + time + " seconds";
I still see a lot of String allocations being made.
At least you can use String.format() to reduce the number of created objects:
mEventUpdate = String.format("Time elapsed is %d seconds", time);
A String in Java is an unmutable object. Once created, you cannot change it any more. So if it really has to be a String there is no way to avoid the allocations.
Use StringBuffer
If you have a method returning a string, and you know that its result
will always be appended to a StringBuffer anyway, change your
signature and implementation so that the function does the append
directly, instead of creating a short-lived temporary object.
http://developer.android.com/training/articles/perf-tips.html#ObjectCreation
I wont bother with a long answer, either use format like someone said or use the often-overlooked StringBuffer - which I use when joining large number of Strings together, say in a loop, where using format wouldn't be possible.
http://developer.android.com/reference/java/lang/StringBuffer.html
(I like Android's reference because look it compared to that nasty Oracle's nasty one :P)
Declare this as global static variable
public static StringBuilder mEventUpdate = new StringBuilder();
mEventUpdate.delete(0, buffer.length());
mEventUpdate.append("Time elapsed is ");
mEventUpdate.append(time);
mEventUpdate.append(" seconds");
// TO desplay
mEventUpdate.toString();

Why does this trigger a force close on Android?

Why does this code trigger a force close in Android?
`score.setText(Integer.parseInt((String) score.getText())+1);`
score is a TextView, and I am simply increasing the number by 1. I have predefined a String resource to be the initial number in the score TextView.
I am quite frustrated.
First off you should try breaking down your code so you can actually see what is going on with it.
Instead of
score.setText(Integer.parseInt((String) score.getText())+1);
try
String tmp = score.getText().toString();
int score;
score = Integer.parseInt(tmp) + 1;
score.setText(String.valueOf(score));
EDIT: Upon further reading of the documentation, setText has several overloads, one of which DOES take an int, but it takes the int of a resource ID. My guess is that your score is not a valid resource ID, thus crashing your application.
public final void setText (int resid)
Oh and as far as the frequent FC's when beginning Android Dev, it happens to the best of us. The key is to learn WHY the FC's happen, and have a LOT of patience.
mostly u need to do this
score.setText(Integer.parseInt(score.getText().toString())+1);
coz.. getText() returns a Editable Object which cannot be parsed to Integer. So it give NumberFormat Exception.
AndMake sure to set TextView,s Text to an integer initially..
try this way
score.setText(String.valueOf(Integer.parseInt(score.getText().toString())+1));
as you can pass the integer value that's why getting force the application
TextEdit.setText takes a CharSequence as input.
You are supplying an integer through Integer.parseInt((String) score.getText())+1
See, if converting it back to string and using it in setText helps.
You can convert an integer to string using Integer.toString.
PS: I am new to java myself.
The compiler should have ideally caught this error.
It's possible java uses some implicit type conversions from string to int.

Removing notification using String tag and int id?

I'd like to put a notification in the notification tray. It's for a multiplayer game, and I want to notify a user when it's their turn to make a move. They can participate in multiple games at once, and I'll send one notification per game when/if it's their move. Each game has a unique ID, so I wanted to send notifications like:
int TYPE_NOTIF_YOUR_TURN = 0;
String gameId1 = "abc";
String gameId2 = "xyz";
mgr.notify(gameId1, TYPE_NOTIF_YOUR_TURN, notification);
mgr.notify(gameId2, TYPE_NOTIF_YOUR_TURN, notification);
Now if I need to cancel one of the notifications, I can look up using the tag (in my case the gameId)?:
// only remove the second notification:
mgr.cancel(gameId2, TYPE_NOTIF_YOUR_TURN);
Is that how it works?
Thanks
Yes that's exactly how it works.
On a side note though, I would make the String tags unique to your app package. Example...
String gameId1 = "com.mycompany.mypackage.gameid.abc";
String gameId2 = "com.mycompany.mypackage.gameid.xyz";
The reasoning behind this is that when cancelling, the string and the int must match to identify the notification to cancel. Many apps are likely to use int values of 1, 2, 3 etc. So the simpler the string tag, the more likely another app (using simple tags) might accidentally cancel one of your notifications.
In general it seems fairly unlikely but unique tags will avoid it. It also seems likely that this would be recommended practice though I haven't looked into it much.

Categories

Resources