简体   繁体   English

前台服务在通知单击时被杀死

[英]Foreground Service being killed on Notification click

In Android 4.4.2 clicking on my Foreground Service notification is killing my process. 在Android 4.4.2中,单击“前台服务”通知将终止我的进程。

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. 在较旧的设备上(Samsuing选项卡2运行4.2.2),我可以从“近期任务”中清除“活动”,并且仍然可以使我的Service在后台正常运行。 Then when I click on the Notification my app Activity starts again quite happily. 然后,当我单击“ Notification我的应用程序Activity再次很愉快地开始。

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). 但是,一旦我在运行4.4.2的Nexus 7上单击通知,我的进程就会被终止(直到单击在后台愉快地运行)。 The PendingIntent doesn't seem to fire at all, or at least, it doesn't hit the first line of the BroadcastReceiver : PendingIntent似乎根本没有触发,或者至少没有到达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. 我已经通过运行这个答案,并且使用命令dumpsys activity proccesses我已经证实了我的服务前台运行正常。

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() ) 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. FLAG_RECEIVER_FOREGROUND标志添加到您的通知中使用的PendingIntent中,以允许服务以Foreground优先级运行。

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 . 这是此信息的来源: Android问题跟踪工具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM