关于android service的问题

在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);