Issue
Hey this was probably asked a couple of times but I can't seem to find it.
Androids C2DM service allows to send message to an application when it's not active right? So it's useful to send notification from the server. The thing is, when the user is using the app and there is an update From the server, how should I notify him? I can use a refresh button but I rather not.
So my question really is how can that be achieved?
How does the C2DM service acts if the user is online? Does it work the same can I just handle that event to update the content?
Or should I use some sort of background thread/ AsyncTask / Service? I'm not sure which one of them is right for the task.
Logically it seems inefficient to reach the server with an http request every couple of seconds, so the C2DM seems the better choice if it's possible.
Any thoughts on the matter?
Solution
Androids C2DM service allows to send message to an application, whether or not app is active. That is, the device can receive a message even if user is currently using the app, or the app is running in background.
When you receive a message from C2DM server, first check in your C2DMReceiver class, if your app is running in background, if yes then show the message in notification (status bar) otherwise you can show the message via a Toast or AlertDialog box.
The following code can check if App is running in background:
public static boolean isApplicationInBackground(final Context context) {
final ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
final ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
Thank you :)
Answered By - Shrikant Ballal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.