當內存不足時,Android會殺死一些服務。如何防止Android顯示應用程序停止消息
像這樣:
我知道我可以使用前臺服務,禁止機器人殺死我的服務
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
try {
Notification notification = new Notification(R.mipmap.ic_launcher,"this is service", System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent , 0);
notification.setLatestEventInfo(this, "myapp", "myservice", contentIntent);
notification.flags =Notification.FLAG_AUTO_CANCEL;
startForeground(123,notification);
}
catch(Exception e)
{
stopSelf();
}
}
@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
}
但是,這將在屏幕上
我顯示通知寧願殺服務比顯示通知,但我也不想顯示停止的消息。
我發現了一些應用程序,當android殺死它時,它不會顯示任何消息。
例如Screen Dimmer
如何禁止android顯示應用程序停止的消息?