应用程序被杀后Android服务停止
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了应用程序被杀后Android服务停止,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1581字,纯文字阅读大概需要3分钟。
内容图文
我想创建一个即使从任务管理器关闭应用程序也能运行的服务.我创建了一个服务,然后记录了一条消息以检查它是否正在运行,我注意到它只有在应用程序运行或处于前台时才有效.
服务类:
public class CallService extends Service {
private final LocalBinder mBinder = new LocalBinder();
protected Handler handler;
public class LocalBinder extends Binder {
public CallService getService() {
return CallService .this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("TESTINGSERVICE", "Service is running");
}
}
从我的MainActivity启动服务:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
startService(new Intent(this, CallService.class));
表现
<application>
...
<service
android:name=".activities.services.CallService">
</service>
</application>
我需要做些什么改变?谢谢,伙计们
解决方法:
在您的服务中,添加以下代码.
@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);
}
内容总结
以上是互联网集市为您收集整理的应用程序被杀后Android服务停止全部内容,希望文章能够帮你解决应用程序被杀后Android服务停止所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。