Foreground Service being killed on Notification click - android

In Android 4.4.2 clicking on my Foreground Service notification is killing my process.
On older devices (Samsuing Tab 2 running 4.2.2), I can swipe away the Activity from Recent Tasks and still have my Service running fine in the background. Then when I click on the Notification my app Activity starts again quite happily.
However, once I click the Notification on my Nexus 7 running 4.4.2 my process is killed (which up until the click is running happily in the background). The PendingIntent doesn't seem to fire at all, or at least, it doesn't hit the first line of the BroadcastReceiver:
05-21 16:17:38.939: I/ActivityManager(522): Killing 2268:com.test.student/u0a242 (adj 0): remove task
I've run through this answer, and using the command dumpsys activity proccesses I've confirmed that my Service is running in the foreground correctly.
So, what is it about clicking this Notification which is killing my process?
Code involved in moving the Service to the Foreground follows:
Service:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("NativeWrappingService", "Starting Service...");
startForeground(NotificationIcon.NOTIFICATION_STUDENT, NotificationManager.getStudentIcon(this).getNotification());
return super.onStartCommand(intent, flags, startId);
}
NotificationIcon: (getStudentIcon(this).getNotification())
public Notification getNotification() {
Builder mBuilder = new Builder(mContext);
if(mSmallIcon != -1) mBuilder.setSmallIcon(mSmallIcon);
if(mLargeIcon != null) mBuilder.setLargeIcon(mLargeIcon);
if(mTitle != null) mBuilder.setContentTitle(mTitle);
if(mSubTitle != null) mBuilder.setContentText(mSubTitle);
if(mSubTitleExtra != null) mBuilder.setContentInfo(mSubTitleExtra);
mBuilder.setOngoing(mOngoing);
mBuilder.setAutoCancel(mAutoCancel);
mBuilder.setContentIntent(getPendingIntent(mContext, mAction, mBundle, mActivity));
return mBuilder.build();
}
private PendingIntent getPendingIntent(Context context, String action, Bundle extras, String activity) {
Intent newIntent = new Intent(context, BroadcastToOrderedBroadcast.class);
Bundle bundle;
if(extras != null) bundle = extras;
else bundle = new Bundle();
if(activity != null && !activity.equalsIgnoreCase("")) {
BundleUtils.addActivityToBundle(bundle, activity);
}
BundleUtils.addActionToBundle(bundle, action);
newIntent.putExtras(bundle);
return PendingIntent.getBroadcast(NativeService.getInstance(), mNotificationID, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}

Add FLAG_RECEIVER_FOREGROUND flag to the PendingIntent used from your notification in order to allow the Service to run at Foreground priority.
Intent newIntent = new Intent(context, BroadcastToOrderedBroadcast.class);
newIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
return PendingIntent.getBroadcast(NativeService.getInstance(), mNotificationID, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Here is the source of this information : Android Issue Tracker tool.

Related

App Getting Killed on Certain Android Oreo Phones AFTER ~20 MINUTES

i am testing my application and noticed that it is getting killed by the OS (ONLY ON OREO DEVICES) on certain devices (ON Samsung S8 there is no problem at all but on Huawei Y7 Prime the application is restarting when i open it again after having the screen off for more than 20 minutes..noting that in case i open the application directly after without waiting few minutes, then the application will open normally)
i also performed some logging to identify when and why the activity is getting destroyed but with no luck since there are no logs being written in this situation. also, i added the following code on the main activity which worked for the short time (pressing on the app icon first restarted the app on some devices but was fixed using the below code in onCreate function of the Main Activity)
if (!isTaskRoot()) {
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
isTaskRootEntered = true;
finish();
}
}
am i missing something? please any help regarding how to fix this case would be appreciated.
IMPORTANT NOTE: i have a foreground service that is kept on which retreives the user's location and saves it in a database. this service has a sticky notification and its working well until left for more than 20 minutes (AGAIN ONLY ON SOME DEVICES LIKE HUAWEI)
i am starting the foreground service the following way:
GVGPSServiceIntent = new Intent(context, GTMStarterService.class);
if(Build.VERSION.SDK_INT >= 26){
context.startForegroundService(GVGPSServiceIntent);
}
else context.startService(GVGPSServiceIntent);
the service looks like this
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
#Override
public void onCreate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification notification = updateNotificationContent();
startForeground("SendingPrcs".hashCode(), notification);
}
}
#Override
public void onDestroy() {
super.onDestroy();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.cancel("SendingPrcs".hashCode());
}
}

Android IntentService not completing task if App is closed

I'm using an IntentService to saveData to a SQLDatabase & upload data to Parse. The IntentService works fine if I keep the app running. It also executes propery if I minimise it(by pressing the home button).
But if I press the navigation button and destory the app(the thing where the app screen scales down and you swipe it off); then the IntentService stops running(it doesnt even call onDestory).
What I want is to have the service execute all the tasks; and destory itself. Hence, START_STICKY (Intent) is not required as I dont want it to run continuously in the background.
Starting IntentService
Intent intent_publishService = new Intent(getApplicationContext(), PublishService.class);
Bundle basket_storyData = new Bundle();
basket_storyData.putAll( packStoryData() );
intent_publishService.putExtra(KEY_BASKET_STORY_DATA, basket_storyData);
intent_publishService.putParcelableArrayListExtra(KEY_BASKET_IMAGE_DATA, (ArrayList<? extends Parcelable>) final_image_data);
startService(intent_publishService);
PublishService.class (Snippet)
#Override
protected void onHandleIntent(Intent intent)
{
Log.i(TAG, "ONHANDLEINTENT" );
context = getApplicationContext();
final String KEY_BASKET_IMAGE_DATA = "key_basket_image_data";
final String KEY_BASKET_STORY_DATA = "key_basket_story_data";
final String KEY_BASKET_EXTRA = "key_basket_extra";
ArrayList<ImagesData> _iDataSave = new ArrayList<ImagesData>();
_iDataSave.addAll( (Collection<? extends ImagesData>) intent.getParcelableArrayListExtra(KEY_BASKET_IMAGE_DATA) );
Log.i(TAG, "_iDataSize:" + Integer.toString(_iDataSave.size()) );
//Get Bundle Values
Bundle storyBundle = intent.getBundleExtra(KEY_BASKET_STORY_DATA);
publishStory(storyBundle, _iDataSave);
}
Edit1: Manifest Declaration
<service android:name="com.example.create.main.PublishService" />
SOLVED
You will need START_STICKY in this case to indicate to android that you wish to try to continue to do work after the app is killed.
When the user swipes away from the app chooser, that will kill the app process unconditionally. You can't prevent that from happening - it is intended to give the user control over what is running at that very moment.
Here's what needs to be done:
1) Change IntentService >> Service
2) Move all the code to onStartCommand
3) Using a separate thread so that everything is done off the UI-Thread
4) Declaring it as a separate process in the Manifest
PublishService.class (Snippet)
#Override
public int onStartCommand(final Intent intent, int flags, int startId)
{
Log.i(TAG, "ONSTARTCOMMAND" );
Intent notificationIntent = new Intent(this, MyFriendList_Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("WhatsApp Notification");
builder.setContentText("You have a new message");
builder.setSmallIcon(R.drawable.ic_launcher);
//builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setNumber(100);
// Sets the progress indicator to a max value, the current completion
// percentage, and "determinate" state
builder.setProgress(0, 0, true);
Notification notificationFinal = builder.getNotification();
startForeground(ONGOING_NOTIFICATION_ID, notificationFinal );
Runnable r = new Runnable()
{
public void run()
{
context = getApplicationContext();
final String KEY_BASKET_IMAGE_DATA = "key_basket_image_data";
final String KEY_BASKET_STORY_DATA = "key_basket_story_data";
final String KEY_BASKET_EXTRA = "key_basket_extra";
ArrayList<ImagesData> _iDataSave = new ArrayList<ImagesData>();
_iDataSave.addAll( (Collection<? extends ImagesData>) intent.getParcelableArrayListExtra(KEY_BASKET_IMAGE_DATA) );
Log.i(TAG, "_iDataSize:" + Integer.toString(_iDataSave.size()) );
//Get Bundle Values
Bundle storyBundle = intent.getBundleExtra(KEY_BASKET_STORY_DATA);
publishStory(storyBundle, _iDataSave);
}
};
Thread t = new Thread(r);
t.start();
//return START_STICKY;
//return START_REDELIVER_INTENT;
//return START_CONTINUATION_MASK;
return START_NOT_STICKY;
}
Manifest Declaration
<service
android:name="com.example.create.main.PublishService"
android:process=".publishservice" />
Concern
startForeground() + separate Process + running on a separate Thread
--- maybe all 3-are not required?
Edit 1: Also; you could skip all the foreground stuff; and return START_CONTINUATION_MASK ; but thats not recommended as every device has a different way of handling that return type

Resume singleTask activity

I am trying to "resume" a single task activity so it appears in the foreground when a user clicks my notification. (Same behavior as if the user tapped on the app icon from the applications menu.)
My notification creates a PendingIntent which broadcasts an action that is received by my broadcast receiver. If the app is in not in the foreground, I try to resume the app. Additionally, I'm trying to pass a message to my onResume function through the intent. However, I'm hitting an error:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Despite this error, my app is being resumed...don't understand why. However, my extras are not being passed to my onResume function.
So first I create a notification.
public static class MyNotificationCreator {
private static final int MY_NOTIFICATION_ID = 987;
public static void createNotification(Context context) {
Intent openAppIntent = new Intent(context, MyReceiver.class);
openAppIntent.setAction("PleaseOpenApp");
PendingIntent pi = PendingIntent.getBroadcast(context, /*requestCode*/0, openAppIntent, /*flags*/0);
Notification notification = ne Notification.Builder(context)
.setContentTitle("")
.setContentText("Open app")
.setSmallIcon(context.getApplicationInfo().icon)
.setContentIntent(pi)
.build();
NotificationManager notificationManager = (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, notification); }
}
Which broadcasts "PleaseOpenApp" for MyReceiver.
public class MyReceiver extends BroadcastReceiver {
#Override
public void onRecieve(Context context, Intent intent) {
if (intent.action() == "PleaseOpenApp" && !MyPlugin.isForeground) {
PackageManager pm = context.getPackageManager();
//Perhaps I'm not supposed to use a "launch" intent?
Intent launchIntent = pm.getLaunchIntentForPackage(context.getPackageName());
//I'm adding the FLAG_ACTIVITY_NEW_TASK, but I'm still hitting an error saying my intent does not have the FLAG_ACTIVITY_NEW_TASK...
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchIntent.putExtra("foo", "bar");
context.startActivity(launchActivity);
} else {
//do other stuff
}
}
}
My plugin keeps track of whether or not we're in the foreground. Also, it tries to get "food" after my receiver attempts to start the app.
public class MyPlugin extends CordovaPlugin {
public static boolean isForeground = false;
#Override
public void initialize(CordovaInterface cordova, CordovaWebView webview) {
super.initialize(cordova, webview);
isForeground = true;
}
#Override
public void onResume(boolean multitasking) {
isForeground = true;
String foo = activity.getIntent().getStringExtra("foo");
Log.d("MyPlugin", foo); //foo is null after clicking the notification!
}
#Override
public void onPause(boolean multitasking) {
isForeground = false;
}
#Override
public void onDestroy() {
isForeground = false;
}
}
Note: because I'm using cordova my activity has a singleTask launchMode.
Also, I'm new to Android development so any help about resuming activities not in the foreground vs resuming activities that have been destroyed and info about general concepts / best practices that I'm not understanding would be appreciated!
I don't think your Broadcast/Broadcast Receiver pattern is necessary.
Intents can be used to directly launch an activity, and when you build the Intent, you can add the extras. Then, your activity onResume() can extract them directly.
Here is a sample Intent and PendingIntent construction that can be sent in a notification:
Intent startActivity = new Intent(context, MyActivity.class);
// You can experiment with the FLAGs passed here to see what they change
startActivity.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("Extra1", myExtra1)
.putExtra("Extra2", myExtra2)
// ADDING THIS MAKES SURE THE EXTRAS ATTACH
.setAction("SomeString");
// Then, create the PendingIntent
// You can experiment with the FLAG passed here to see what it changes
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, startActivity, PendingIntent.FLAG_UPDATE_CURRENT);
// Then, create and show the notification
Notification notif = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.my_small_icon)
.setContentTitle(myTitle)
.setContentText(myContent)
.setOngoing(isOngoingNotif)
.setAutoCancel(shouldAutoCancel)
.setOnlyAlertOnce(shouldAlertOnce)
.setContentIntent(pendingIntent)
.build();
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(MY_NOTIFICATION_ID, notif);
In your code you are using a "launch Intent" to resume your application. You've added "extras" to the Intent but they will never be seen.
If your app is running, but in the background, and you call startActivity() with a "launch Intent", all this does it bring your task from the background to the foreground. It does not deliver the Intent to the Activity!.
A "launch Intent" does exactly the same thing as when you press the app icon of an app on the HOME screen (if it is already running, but in the background). This just brings the existing task in its current state, from the background to the foreground.
If you want to delivery "extras" to your app, you cannot use a "launch Intent". You must use a regular 'Intent. Depending on your architecture, you could either start a newActivity(which would get the "extras" inonCreate(), or you could start an existingActivity(which would get the "extras" inonNewIntent()`.

Stopping a background service from notification

I have a background service in which I want to show a notification which allows the user to stop it.
In the android SDK docs it says an activity is used to normally launch an Activity. So I am wondering if I need to create an activity to stop the service or can I directly stop the service when user selects the notification,
So how would the intend call back the service to stop it..
Thanks,
So I am wondering if I need to create an activity to stop the service or can I directly stop the service when user selects the notification,
You cannot directly stop the service from a Notification. You can start the service, using an Intent that has an action string or extra or something that the service sees in onStartCommand() and triggers it to call stopSelf().
The question is already old, but since there is still no solution with code, I simply share my code as an example for solving the problem:
You cannot directly stop the service from a Notification. You can
start the service, using an Intent that has an action string or extra
or something that the service sees in onStartCommand() and triggers it
to call stopSelf().
That's the right solution so let's jump in code (this code is all in your ExampleService class):
#RequiresApi(Build.VERSION_CODES.O)
private void startForegroundService() {
// create PendingIntend to open MainActivity (this is when the notification gets clicked) //
Intent tabIntent = new Intent(this, MainActivity.class);
tabIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent tabPendingIntent = PendingIntent.getActivity(this, 0, tabIntent, 0);
// create PendingIntend to open ExampleService (this is when the notification BUTTON gets clicked) //
Intent closeIntent = new Intent(this, ExampleService.class);
closeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
closeIntent.putExtra("destroyCode", 666); // this is the important line //
PendingIntent closePendingIntent = PendingIntent.getService(this, 0, closeIntent, 0);
createNotificationChannel(); // this is only the default code to create notification channel. I just outsourced? it //
Now the Intent has additional data (the "destroy code" -> 666). Notice that we have created 2 pendingIntents: closePendingIntent (stop Service) and tabPendingIntent (start Activity)
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
// get extras to know if Intent has destroyCode (666)
Bundle extras = intent.getExtras();
if (extras == null) {
// extras is null which means there is no destroyCode (666)
exampleMethod();
} else {
// Intent has destroyCode (666) -> Intent comes from notification -> stop the service and close notification
stopSelf();
}
return START_STICKY;
}
Now we have the code to check if there is a destroyCode or not. The last step is to create a notification with a button:
// set attributes for notification //
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channelID_2");
Notification notification = builder.setOngoing(true)
.setSmallIcon(R.drawable.example)
.setContentTitle(getText(R.string.notificationTitle))
.setContentText(getText(R.string.notificationText))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(tabPendingIntent) //this is when notification is clicked which only opens ExampleActivity
.addAction(R.drawable.example, getString(R.string.notificationButtonText), closePendingIntent) // here is our closePendingIntent with the destroyCode .addAction is "the onClickListener for the notification button"//
.build();
startForeground(2, notification);
In onCreate you start your service
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
startForegroundService();
else
startForeground(1, new Notification());
// Toast Message that service has started
Toast.makeText(this, R.string.serviceStarted, Toast.LENGTH_SHORT).show();
That's it
You can't start an Acitivty from a Service just like that. What you can do is create a callback to an Activity in the Service and let the callback start new activities. But having a notification means you don't have to go through the Service. When the notification is clicked, you can start an activity that's specified in the Intent you supply to the notification. It's really very simple.
Do read the reference docs on notifications for examples.

Stopping IntentService by clicking Notification

I'm doing some background work in an IntentService and trying to make it stop by clicking a notification. For stopping the work I have a static method, that sets a flag.
public static void stopService() {
if (task != null) {
task.setCancelFlag(true);
}
}
The notification has a PendingIntent, that sends a Broadcast to a Receiver, that attempts to stop the service.
Intent intent = new Intent(this, AlarmReceiver.class);
intent.setAction(AlarmReceiver.STOP_SERVICE);
PendingIntent contentIntent = PendingIntent.getBroadcast(getBaseContext(), 0,
intent, 0);
notification.contentIntent = contentIntent;
The Receiver calls the stopService() method when it receives a broadcast.
if (intent.getAction().equals(STOP_SERVICE)) {
UpdateCheckService.stopService();
}
Strangely enough, the stopService() method is not called properly. If I try to log it, the part with the flag setting is not executed. Even if I set a breakpoint on the Receiver and try to debug it, it doesn't work.
However, if I call the same method from an Activity by clicking a button, everything works as intended.
Does somebody know, where this strange behavior comes from?
I did it using the intent of IntentService to create the PendingIntent
In onHandleIntent I invoke the notification :
#Override
protected void onHandleIntent(Intent intent) {
PendingIntent pStopSelf = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Then I added the stop button and call my notification (insert this line on NotificationCompat.Builder):
.addAction(R.drawable.ic_close_white_24dp, "Stop", pStopSelf)
Clicking the stop on the notification will not trigger onHandleIntent, but invoke onStartCommand. Here you can check if the intent contains the flag we set to stop the service.
private boolean shouldStop = false;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getFlags() == PendingIntent.FLAG_CANCEL_CURRENT) {
Log.d(TAG, "Stop pressed");
stopSelf();
shouldStop = true;
return Service.START_NOT_STICKY;
}
return super.onStartCommand(intent, flags, startId);
}
stopSelf() stops any intents containing work requests to do more work in the intent service to restart the service. But it does not stop the service itself.
To stop the service from continuing to execute, use the boolean previously set to check if the work should continue like this in onHandleIntent()
#Override
protected void onHandleIntent(Intent intent) {
//Invoke notification
doStuff();
}
private void doStuff() {
// do something
// check the condition
if (shouldContinue == false) {
return;
}
}
Use the boolean to check in between the code to check if service should stop and return from the method.
You should not use the IntentService class for your case. IntentService use a queue to process one Intent at a time. Just create your own class that extend Service as the example here.
Then handle the stop request as you did to stop the worker thread.
The mystery is solved: My BroadcastReceiver had the remote process flag set, which I copied from some tutorial on the web without too much thinking. Removing this tag made everything work as expected.

Categories

Resources