Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 前景服务在通知点击时被杀死大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Android 4.4.2中,单击我的Foreground service通知会终止我的进程.

在较旧的设备上(Samsuing Tab 2运行4.2.2),我可以从最近的任务中删除活动,并且仍然可以在后台运行我的服务.然后,当我点击通知时,我的应用程序活动再次开心.

但是,一旦我点击运行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,BroadcastTo@R_874_5316@Broadcast.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);
}
最佳答案
FLAG_RECEIVER_FOREGROUND标志添加到您的通知中使用的PendingIntent,以便允许服务以Foreground优先级运行.

Intent newIntent = new Intent(context,BroadcastTo@R_874_5316@Broadcast.class);
newIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
return PendingIntent.getBroadcast(Nativeservice.geTinstance(),PendingIntent.FLAG_updatE_CURRENT);

以下是此信息的来源:Android Issue Tracker tool.

大佬总结

以上是大佬教程为你收集整理的android – 前景服务在通知点击时被杀死全部内容,希望文章能够帮你解决android – 前景服务在通知点击时被杀死所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。