后台服务停止活动时杀死或停止或强制关闭 [英] Background Service Stopped When Activity Killed Or Stop or force close

查看:395
本文介绍了后台服务停止活动时杀死或停止或强制关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,当IM pressing主页按钮并关闭我的应用程序在那个时候后台服务采空而之后,它需要重新登录在应用程序,然后后台服务将因此开始我怎么可以这样做Whatssapp如果应用程序将关闭或Kiiled随后还SEVICE可以在后台运行.....

My Problem Is When I M Pressing Home Button And Close My App At That Time Background Service stoped And After That It Requires To LOgin Again In App Then Background Service Will Start So How Can I Do Like Whatssapp If App Will Close Or Kiiled Then Also Sevice Can Run In Background.....

推荐答案

我有同样的问题。当你刷卡应用了应用程序托盘,后台服务的过程中被杀死。

I had the same problem. When you swipe an application out of the application tray, the process of the background service is killed.

您可以在自己的服务中添加以下code或看<一个href=\"http://stackoverflow.com/questions/20592366/the-process-of-the-service-is-killed-after-the-application-is-removed-from-the-a\">this问题

You can add the following code in your service or see this question

@Override
public void onTaskRemoved(Intent rootIntent){
   Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
   restartServiceIntent.setPackage(getPackageName());

   PendingIntent restartServicePendingIntent =  PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
   AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
   alarmService.set(
      AlarmManager.ELAPSED_REALTIME,
      SystemClock.elapsedRealtime() + 1000,
      restartServicePendingIntent);

   super.onTaskRemoved(rootIntent);
}

这是什么code的作用是,它重新启动应用程序的过程中一旦当你的活动是杀了它被杀死。

What this code does is that it restarts the process of your application once it is killed when your activity is killed.

这篇关于后台服务停止活动时杀死或停止或强制关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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