问题描述
- 关于android service的问题 3C
- 在activity中使用startservice启动service,然后长按home键,清除后台该程序,但是service也被删掉了,在模拟器中不会被删掉,但是真机就被删掉了,请问为什么
解决方案
还是用真机调试比较好;可以写一个BrocastReceiver,在Service销毁回调事件中发送广播,广播接收器重新启动Service
解决方案二:
你真机中清理,会把一些相关的停止。模拟器中应该没有清理吧。
解决方案三:
如果需要实现service长期留存后台,可以考虑通用做法:1.两个service相互监听,一个被杀掉另一个立刻启动;2.让service每隔几分钟自动重启一次。
解决方案四:
清除后,整个程序都被杀掉了,service随着立刻就消失
解决方案五:
真机是原生系统吗?深度定制系统有的有后台自动清理功能
解决方案六:
机器问题,一些定制系统会强行杀死后台应用,如小米。
解决方案七:
放到前台吧,或者给进程加锁
Notification notification = new Notification(R.drawable.icon getText(R.string.ticker_text) System.currentTimeMillis());Intent notificationIntent = new Intent(this ExampleActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this 0 notificationIntent 0);notification.setLatestEventInfo(this getText(R.string.notification_title) getText(R.string.notification_message) pendingIntent);startForeground(ONGOING_NOTIFICATION notification);
时间: 2024-07-28 19:53:54