我有一个在后台运行的服务。 他返回START_STICKY因为它监听的一些事件,这个事件(来自传感器的数据)出现非常快。 我有些情况下我的服务被杀害:我有一个空的意图,当我的服务已杀死,因为他的过程中消失根据文档它可以发生。
http://developer.android.com/reference/android/app/Service.html#onStart%28android.content.Intent,%20int%29
我想知道如何避免它。 任何帮助将不胜感激。
更新
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);
}