From docs I understand that our program goes to background when we open another_one then after a while android will call onDestroy() to take back it's resources ; But our program is kind of watcher (imagine we have a mediaplyer which playes music in backGround) and it should not be closed until int programState == 1 (There is a power button which ends the player or watcher ) . Besides I learned that we can stay foreground with "Notification" which is so cool (But I don't know how) ! Here is a notification code:
in Activity's onCreate(){
//> reading some data and define some initial values .
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification app_notfiy =addNotification();
//> still should perform somehing ?!?
}
private Notification addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_new)
.setContentTitle(getText(string.app_name))
.setContentText("> This is a notification !?")
.setUsesChronometer(true)
;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
return builder.build() ;
}
But here we have 2 problems . First I don't think notification is really attached to Activity (Maybe it needs some permissions or use service not sure !? ) ! Next I want notification to resume app not restart it !? (I think it starts activity from onCreate() ) How force it back to where we were ?
I've searched alot but still couldn't find good answers about this . Any Idea ?
To achieve this you have to use a Service. Have a look at this tutorial.
You can start the service as a "foreground service", which runs in the background and will hardly ever get killed by the system.
To do this, call startForeground(id, notification).
Related
I had this piece of code that was working nicely not long ago:
Intent browserAction = new Intent(Intent.ACTION_VIEW, uri);
browserAction.setData(uri);
browserAction.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(browserAction);
It's inside onReceive of a BroadcastReceiver, to trigger the browser from a notification action (a PendingIntent that also do other things). For some reason (android update maybe) it now only works when I read the notification with my app in the foreground. If I'm outside my app and click the notification action, the browser isn't being called.
Any ideas of what may be happening and what I should check?
EDIT: If I do a PendingIntent directly from Intent.ACTION_VIEW (instead of using Intent.ACTION_VIEW inside a BroadcastReceiver) the action is fired nicely even outside the app. But I can't rely on this since my BroadcastReceiver did other things after calling the browser.
You need to use pending intent instead of simple intent. because notification only triggers pending intents usually while your app is in the background.
so, please try this code.
Intent browserAction = new Intent(Intent.ACTION_VIEW, uri);
browserAction.setData(uri);
browserAction.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Create the PendingIntent
PendingIntent notifyPendingIntent = PendingIntent.getActivity(
this, 0, browserAction, PendingIntent.FLAG_UPDATE_CURRENT
);
UPDATE
this answer will be useful for you. please refer to this.
refer this
I have some code that sends out a notification that a download is completed, when this notification is clicked, I want it to restart my activity.
Currently, the notification is created in a BroadcastReceiver. This is the section where I added the Intents to the notification.
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(context, CurrentActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
PendingIntent resultPendingIntent = PendingIntent.getActivity(
context,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setContentIntent(resultPendingIntent);
Right now what this does is destroy my CurrentActivity by calling the onDestroy() method, then calls onCreate() to create a new instance. This is an issue for me as my onDestroy() unbinds my BoundService from my activity. I would much rather have my Activity just restart (onStop() then onRestart() then onStart()) so my onCreate() and onDestroy() methods can handle the binding and unbinding and my onStart() method handles the changes to the UI.
Is there a way to do this?
I think you might consider setting the intent flags differently to achieve the behavior that you want.
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Handle your UI changes in your onResume activity if required. Hope that helps.
Try to set your activity flag to be
resultIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
See the flag official document from here: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
If set, and this activity is either being started in a new task or bringing to the top an existing task, then it will be launched as the front door of the task. This will result in the application of any affinities needed to have that task in the proper state (either moving activities to or from it), or simply resetting that task to its initial state if needed.
Hi all,
I am implementing a notification in a service which when clicking on the notification once it appears - It opens an activity.
I have this code in my service class inside a runnable that runs nonstop every few seconds:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_email)
.setContentTitle(notificationTitle)
.setContentText(notificationMessage);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
builder.setStyle(bigText);
Intent resultIntent = new Intent(this, openemailactivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, notification);
In my notification activity named "openemailactivity" I have a button which has a code in it to return the user to the home activity called "MainActivity":
btnReturnToMainScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
Everything works fine until I restart my Android phone, the service loads on boot (as intended) and then generates a notification and then I click on it.
Then, when I click on it, the "openemailactivity" activity opens well also as intended, and then I click the button that opens the "MainActivity" (The above code) and it does that O.k.
And now the problem starts !
After this other activity was opened ("MainActivity"), and then the notification pops up again by the service (Notification timer\checking runs in a timed Handler\Runnable that runs nonstop) - and then I click on the notification itself that was created -
the activity that it should call ("openemailactivity") is not called anymore ! instead - the last called activity pops up ("MainActivity"), which is wrong !
The problem starts only when I restart my phone, the service loads on boot, notification is generated, I click on it, It opens the right activity, and then I call the code from the button (OnClickListener).
I don't have a clue on how to solve this problem.
What do you think can be the problem ?
What am I doing wrong and how can I solve it ?
Thanks for any reply.
Strange but it looks like I found an answer (Hopefully - It needs to get more comprehensive testing since Surprises always exist in this environment).
I tried to set:
resultIntent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
in the notification code that exists in the service, right after declaring this variable (resultIntent).
As for now it looks like it works !
By looking at the Android documentation for this flag, it looks like it may explain the problem that I had:
https://developer.android.com/reference/android/content/Intent.html
(Search for the explanation of: "FLAG_ACTIVITY_CLEAR_TOP").
I've read a bunch of answers pertaining to this question and they all seem to be the same:
"Run your service with START_STICKY"
"Run your service in the foreground"
"Run your service with startService and don't bind it"
I'm doing ALL of these things, and my service STILL closes and restarts every time my activity is closed.
This is NOT an IntentService.
I'm also not calling stopSelf or stopService anywhere except in onClick handlers.
Please scroll down to my update - This behavior has been confirmed to be a bug in the android OS and I have reported it to google. Click here to view the report.
Starting my service from MainActivity:
svcIntent = new Intent(getBaseContext(), MyService.class);
startService(svcIntent);
In my onStartCommand:
// Enter foreground state
String title = "Service has been started...";
String subject = "Service is running...";
String body = "Monitoring your battery usage.";
Notification notification = new Notification(R.drawable.theicon, title,
System.currentTimeMillis());
if (prefs.getBoolean("notificationSounds", true))
notification.defaults |= Notification.DEFAULT_SOUND;
else
notification.sound = null;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, subject, body, pendIntent);
startForeground(1500, notification);
At the end of my onStartCommand:
...
// Release WakeLock
wl.release();
return START_STICKY;
UPDATE
I FIGURED OUT WHATS CAUSING IT! But I have no idea how to fix it. In my service i also use an AlarmManager inside my service to set up function calls to the service a specified time away.
// Alarm manager setup for MyService
AlarmManager AM = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
svcIntent1 = new Intent(this, AlarmReceiver.class);
prefs.edit().putInt("initialBatt", initialBatt).apply();
svcIntent1.setAction("com.myApp.servicealarm");
pendingIntent = PendingIntent.getBroadcast(this, 93, svcIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
// Set the alarm
AM.set(AlarmManager.RTC_WAKEUP, timeNow + waitTime, pendingIntent);
I noticed that if I DO NOT comment out the AM.set call to set an alarm, EVEN WITH AN EMPTY onReceive, my service is killed when the alarm goes off, after I swipe my app away in recent apps. If I comment out the set alarm call, then the service is never killed and keeps running after I close my app. What the heck?! I need this alarm for the functionality of my algorithms!!
It's very odd. As soon as the alarm goes off, my debug message does not print, and my service restarts. But the second time around, after the service restarts, the debug message does print and the program executes successfully.
I've tried this and it still happens with a normal broadcast receiver as well. I've also stripped my code down to ONLY the set alarm call from my service and the broadcast receiver, and the same thing occurs so it's not my algorithm. Apparantly if you have a foreground service that sets an alarm, when the alarm goes off your service restarts.
CLOSING
This behavior seems to be caused by a bug in the android OS so I do not expect an answer. If you'd like to see this bug for yourself, click here. I've provided a project that you can compile and reproduce the problem with.
Android kills the process when the broadcast Intent is sent (before it is received/processed in your app).
This is a nasty Android bug, as of 4.4 (API 19).
See https://code.google.com/p/android/issues/detail?id=63618&can=1&q=service%20restart%20broadcast&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars
especially comments #22 and #23
Unfortunately, almost all "open" issues were marked as "obsolete" recently, with the assumption that they were all fixed in Android 5.0. There's no way for a developer to reopen an "obsolete" issue.
EDIT: Add details about foreground broadcast
Based on the information in the linked issue, it looks like adding Intent.FLAG_RECEIVER_FOREGROUND to your broadcast Intent will ensure that the the process does not get killed on the next receipt of a broadcast Intent.
To do this, add:
svcIntent1.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
to the code where you set your alarm in the AlarmManager.
Please read the comments in the linked issue for more details.
Try to run your service in separate process. Define it in your manifest like this:
<service
android:name=".path.to.service.class"
android:process=":my_service"/>
I'm having a bit of a problem, and having looked in many places I can't seem to find an answer.
I have a service, this service checks for notifications on a site and alerts the user of a new notification. When the user clicks this notification, it should bring them to an Activity (I'll call this CommentActivity)
However, if I click on the notification inside of the app, nothing happens. If I click on the notification outside of the app, it opens up the last activity shown before the app was closed.
Here is my code for displaying the notification:
Notice n; //Notification from site
....
Intent nIntent = new Intent(Intent.ACTION_MAIN);
nIntent.setClass(getApplicationContext(), CommentActivity.class);
nIntent.putExtra("activity_id", n.getFeedID());
nIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, nIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(FlankNotificationService.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(n.getTitle())
.setContentText(n.getText())
.setContentInfo(arg0.getNotificationAmount() + " notification" + (arg0.getNotificationAmount() == 1 ? "." : "s."))
.setContentIntent(pIntent);
Notification mm = mBuilder.getNotification();
mm.flags = Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
mm.tickerText = n.getText();
....
mNM.notify(1558, mm);
So instead of opening up the CommentActivity, it will either resume the app (if the app is not opened) or do nothing (if the app is opened)
Thank you.
Edit 1:
Added Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag to Intent. Still does not fix the problem.
Edit 2:
I found the cause of my problem (which was a rather silly cause). When the CommentActivity starts, if it does not get passed any data from either the savedInstance or the Intent.getExtra, then it will just close out. The reason why I did not think of this at first was because it didn't even show the transition. After fixing the Intent.getExtra problem it now works. Thank you for all the answers.
You may want to add the Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag to your Intent. This will ensure that the CommentActivity is the resumed and active Activity when the intent is handled.