5

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);
}
Community
  • 1
  • 1
Graeme
  • 25,714
  • 24
  • 124
  • 186
  • It is a known bug which is being tracked [here](https://code.google.com/p/android/issues/detail?id=53313). Can you try the solution listed in [comment #7](https://code.google.com/p/android/issues/detail?id=53313#c7). – Manish Mulimani May 28 '14 at 14:21

1 Answers1

4

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.

Manish Mulimani
  • 17,535
  • 2
  • 41
  • 60
  • Isn't it equivalent to FLAG_ACTIVITY_NEW_TASK? They both are 0x10000000, can you support the reference that I can set FLAG_RECEIVER_FOREGROUND at all? – Boris Treukhov Feb 03 '16 at 16:40
  • @Manish Mulimani i went through your suggestion. When my app is closed and if i click the notification my service is getting restarted . And the BroadCast receiver is also not working? – Himanshu Mori May 09 '16 at 14:20