前台服务被杀害的通知点击 [英] Foreground Service being killed on Notification click

查看:197
本文介绍了前台服务被杀害的通知点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的是Android 4.4.2点击我的前景服务通知杀死了我的过程。

在旧设备上(Samsuing表2运行4.2.2),我可以从近期任务滑开活动,而且还有我的背景服务运行良好。然后当我点击通知我的应用活动重新开始相当愉快。

但是,一旦我点击我的Nexus通知7 4.4.2运行我的过程被杀死(其中,直到点击在后台运行的愉快)。在的PendingIntent 似乎并没有在各消防,或至少,它没有击中广播接收器

  05-21 16:17:38.939:I / ActivityManager(522):杀死2268:com.test.student / u0a242(形容词0):清除任务

我已经通过运行此回答,并且使用命令 dumpsys活动proccesses 我已经证实了我的服务的的在前台运行正常。

那么,什么是关于单击此通知是杀死我的过程吗?

参与移动服务的前景

code如下:

服务:

  @覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){
    Log.i(NativeWrappingService,启动服务...);
    startForeground(NotificationIcon.NOTIFICATION_STUDENT,NotificationManager.getStudentIcon(本).getNotification());
    返回super.onStartCommand(意向,旗帜,startId);
}

NotificationIcon:( getStudentIcon(本).getNotification()

 公示的GetNotification(){
    建设者mBuilder =新生成器(mContext);    如果(mSmallIcon!= -1)mBuilder.setSmallIcon(mSmallIcon);
    如果(mLargeIcon!= NULL)mBuilder.setLargeIcon(mLargeIcon);
    如果(mTitle!= NULL)mBuilder.setContentTitle(mTitle);
    如果(mSubTitle!= NULL)mBuilder.setContentText(mSubTitle);
    如果(mSubTitleExtra!= NULL)mBuilder.setContentInfo(mSubTitleExtra);    mBuilder.setOngoing(mOngoing);
    mBuilder.setAutoCancel(mAutoCancel);
    mBuilder.setContentIntent(getPendingIntent(mContext,mAction,mBundle,mActivity));    返回mBuilder.build();
}私人的PendingIntent getPendingIntent(上下文的背景下,串动,捆绑临时演员,弦乐活动){
    意图newIntent =新意图(背景下,BroadcastToOrderedBroadcast.class);
    捆束;
    如果(临时演员!= NULL)包=临时演员;
    否则包=新包();    如果(活性=空&放大器;!&放大器;!activity.equalsIgnoreCase()){
        BundleUtils.addActivityToBundle(捆,活动);
    }    BundleUtils.addActionToBundle(捆绑,动作);    newIntent.putExtras(包);    返回PendingIntent.getBroadcast(NativeService.getInstance(),mNotificationID,newIntent,PendingIntent.FLAG_UPDATE_CURRENT);
}


解决方案

添加的 FLAG_RECEIVER_FOREGROUND 标志的从的PendingIntent您的通知使用,以使服务能够在前景优先级运行。

 意图newIntent =新意图(背景下,BroadcastToOrderedBroadcast.class);
newIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
返回PendingIntent.getBroadcast(NativeService.getInstance(),mNotificationID,newIntent,PendingIntent.FLAG_UPDATE_CURRENT);

下面是这些信息的来源:的Andr​​oid问题跟踪工具

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.

这篇关于前台服务被杀害的通知点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆