我有一项在后台运行的服务。他返回 START_STICKY 因为它侦听一些事件,这些事件发生得非常快(来自传感器的数据)。在某些情况下,我的服务被杀死:我的意图为空,根据文档,当我的服务因他的进程消失而被杀死时,可能会发生这种情况。
我想知道如何避免它。任何帮助将不胜感激。
更新
private void startForeground() {
String title = "Warning";//getString(R.string.note_title);
String text = "App still working";//getString(R.string.note_msg);
Intent intent = new Intent(this, DriverActivity.class);
PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);
Bitmap bitmap = BitmapFactory.decodeResource(
getResources(),
R.drawable.ic_launcher);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(bitmap)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(pending);
Notification notification = builder.getNotification();
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(NOTIFICATION_ID, notification);
}