当应用程序被刷掉时前台服务被杀死

编程入门 行业动态 更新时间:2024-10-17 07:32:29
本文介绍了当应用程序被刷掉时前台服务被杀死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望标题本身能说明我的问题是什么.

我有一个在前台运行的 Service.

@Overridepublic int onStartCommand(意图意图,int标志,int startId){NotificationCompat.Builder builder = new NotificationCompat.Builder(这);PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,新意图(这个,WidgetFlashReceiver.class).setAction(Constants.ACTION_NOTIFICATION_CLICK),PendingIntent.FLAG_CANCEL_CURRENT);builder.setContentIntent(pendingIntent);builder.setOngoing(true);builder.setAutoCancel(false);builder.setContentTitle("示例服务");通知通知 = builder.build();通知.icon = R.drawable.ic_launcher;Notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;startForeground(NOTIFICATION_ID,通知);返回 START_STICKY;}

但是当应用程序从最近的任务中被刷掉时,即使它是一个前台服务,这个服务也会被杀死.

感谢任何帮助.

解决方案

看这个 评论关于Android错误,我找到了这个解决方案,为我解决了问题!

AlarmManager almgr = (AlarmManager)MyContext.getSystemService(Context.ALARM_SERVICE);Intent timerIntent = new Intent(MyUniqueLabel);timerIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);PendingIntent pendingOffLoadIntent = PendingIntent.getBroadcast(MyContext, 1, timerIntent, 0);

你必须做这些事情才能让它工作.

1.) 调用 addFlags 和意图并将其传递到 FLAG_RECEIVER_FORGROUND

2.) 在 PendingIntent.getBroadcast 中使用非零请求代码

如果您忽略其中任何一个步骤,它将无法工作.

I hope title itself says what's my question is.

I have a Service which runs in foreground.

@Override public int onStartCommand(Intent intent, int flags, int startId) { NotificationCompat.Builder builder = new NotificationCompat.Builder( this); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, WidgetFlashReceiver.class) .setAction(Constants.ACTION_NOTIFICATION_CLICK), PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(pendingIntent); builder.setOngoing(true); builder.setAutoCancel(false); builder.setContentTitle("Sample Service"); Notification notification = builder.build(); notification.icon = R.drawable.ic_launcher; notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; startForeground(NOTIFICATION_ID, notification); return START_STICKY; }

but this Service getting killed when app is swiped off from recent tasks even though it is a foreground Service.

Any help is appreciated.

解决方案

Looking at this comment about the Android bug, I found this solution that solve the problem for me!

AlarmManager almgr = (AlarmManager)MyContext.getSystemService(Context.ALARM_SERVICE); Intent timerIntent = new Intent(MyUniqueLabel); timerIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); PendingIntent pendingOffLoadIntent = PendingIntent.getBroadcast(MyContext, 1, timerIntent, 0);

you MUST do these things for it to work.

1.) Call addFlags and the intent and pass it in FLAG_RECEIVER_FORGROUND

2.) Use a non-zero request code in PendingIntent.getBroadcast

If you leave any of those steps out it will not work.

更多推荐

当应用程序被刷掉时前台服务被杀死

本文发布于:2023-11-27 07:50:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:被杀   前台   应用程序   被刷掉时

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!