【问题标题】:Foreground Service being killed on Notification click前台服务在通知单击时被杀死
【发布时间】:2014-05-21 15:25:38
【问题描述】:

在 Android 4.4.2 中,单击我的前台服务通知会杀死我的进程。

在较旧的设备上(运行 4.2.2 的三星 Tab 2),我可以从最近的任务中刷掉 Activity,并且我的 Service 仍然在后台运行良好。然后当我点击Notification 时,我的应用程序Activity 又非常愉快地重新启动了。

但是,一旦我在运行 4.4.2 的 Nexus 7 上单击通知,我的进程就会被终止(直到单击在后台愉快地运行)。 PendingIntent 似乎根本没有触发,或者至少,它没有击中BroadcastReceiver 的第一行:

05-21 16:17:38.939: I/ActivityManager(522): Killing 2268:com.test.student/u0a242 (adj 0): remove task

我已经通过this 回答,并使用命令dumpsys activity proccesses 我已经确认我的服务正在在前台正确运行。

那么,单击此通知会杀死我的进程是什么意思?

将服务移至前台所涉及的代码如下:

服务:

@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);
}

【问题讨论】:

  • 这是一个已知的错误,正在跟踪here。你能试试comment #7中列出的解决方案吗?

标签: android service android-notifications foreground-service


【解决方案1】:

将 FLAG_RECEIVER_FOREGROUND 标志添加到通知中使用的 PendingIntent 以允许服务以前台优先级运行。

Intent newIntent = new Intent(context, BroadcastToOrderedBroadcast.class);
newIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
return PendingIntent.getBroadcast(NativeService.getInstance(), mNotificationID, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);

这是此信息的来源:Android Issue Tracker tool。

【讨论】:

  • 不就相当于FLAG_ACTIVITY_NEW_TASK吗?它们都是0x10000000,你能支持我可以设置FLAG_RECEIVER_FOREGROUND的引用吗?
  • @Manish Mulimani 我接受了你的建议。当我的应用程序关闭并且如果我单击通知我的服务将重新启动。而且广播接收器也不工作?
猜你喜欢
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
  • 2021-07-02
  • 1970-01-01
相关资源
最近更新 更多