Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreground Service being killed on Notification click

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);
}
like image 881
Graeme Avatar asked Sep 30 '22 18:09

Graeme


1 Answers

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.

like image 185
Manish Mulimani Avatar answered Oct 21 '22 01:10

Manish Mulimani