英文:
Service is stopped/killed by the system approx 30-40 mins after killing the app, despite being a foreground Service
问题
public class MyService extends Service {
String NOTIFICATION_CHANNEL_ID = "example.permanence";
final Restarter restarter = new Restarter();
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
}
@RequiresApi(Build.VERSION_CODES.O)
private void startMyOwnForeground() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(getApplicationContext(), MyService.class));
} else {
startService(new Intent(getApplicationContext(), MyService.class));
}
String channelName = "Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
Intent showTaskIntent = new Intent(getApplicationContext(), MainActivity.class);
showTaskIntent.setAction(Intent.ACTION_MAIN);
showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);
showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
showTaskIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager = (NotificationManager) getSystemService(NotificationManager.class);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(),NOTIFICATION_CHANNEL_ID);
notificationBuilder.setContentTitle("App is running in background");
notificationBuilder.setContentText("App is running in background");
notificationBuilder.setNumber(103);
notificationBuilder.setSmallIcon(R.drawable.icon);
notificationBuilder.setOngoing(true);
notificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
Notification notification = notificationBuilder.build();
NotificationManager notificationManger =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManger.notify(1, notification);
startForeground(2, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String channelName = "Background Service";
NotificationChannel chan = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan.setLightColor(Color.BLUE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
}
NotificationManager manager = (NotificationManager) getSystemService(NotificationManager.class);
assert manager != null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(chan);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(),NOTIFICATION_CHANNEL_ID);
notificationBuilder.setContentTitle("App is running in background");
notificationBuilder.setContentText("App is running in background");
notificationBuilder.setNumber(108);
notificationBuilder.setSmallIcon(R.drawable.icon);
notificationBuilder.setOngoing(true);
notificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
Notification notification = notificationBuilder.build();
NotificationManager notificationManger =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManger.notify(1, notification);
try {
startForeground(3, notification);
} catch (Exception e) {
e.printStackTrace();
}
super.onStartCommand(intent, flags, startId);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(getApplicationContext(), MyService.class));
} else {
startService(new Intent(getApplicationContext(), MyService.class));
}
registerReceiver(restarter, restarter.getFilter());
//work
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("restartservice");
broadcastIntent.setClass(this, Restarter.class);
this.sendBroadcast(broadcastIntent);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
英文:
I created a foreground Service with persistent notification. I want to run this service 24x7. But my service is stopped/killed by the system approx 30-40 mins after killing the app. What can I do so that my service is not killed by the system. Its really important that service runs 24x7 to collect user data.
Thanks In Advance.
public class MyService extends Service {
String NOTIFICATION_CHANNEL_ID = "example.permanence";
final Restarter restarter = new Restarter();
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
}
@RequiresApi(Build.VERSION_CODES.O)
private void startMyOwnForeground() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(getApplicationContext(), MyService.class));
} else {
startService(new Intent(getApplicationContext(), MyService.class));
}
String channelName = "Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
Intent showTaskIntent = new Intent(getApplicationContext(), MainActivity.class);
showTaskIntent.setAction(Intent.ACTION_MAIN);
showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);
showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
showTaskIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager = (NotificationManager) getSystemService(NotificationManager.class);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(),NOTIFICATION_CHANNEL_ID);
notificationBuilder.setContentTitle("App is running in background");
notificationBuilder.setContentText("App is running in background");
notificationBuilder.setNumber(103);
notificationBuilder.setSmallIcon(R.drawable.icon);
notificationBuilder.setOngoing(true);
notificationBuilder.setPriority(5);
Notification notification = notificationBuilder.build();
NotificationManager notificationManger =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManger.notify(1, notification);
startForeground(2, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String channelName = "Background Service";
NotificationChannel chan = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName,
NotificationManager.IMPORTANCE_DEFAULT);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan.setLightColor(Color.BLUE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
}
NotificationManager manager = (NotificationManager) getSystemService(NotificationManager.class);
assert manager != null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(chan);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(),NOTIFICATION_CHANNEL_ID);
notificationBuilder.setContentTitle("App is running in background");
notificationBuilder.setContentText("App is running in background");
notificationBuilder.setNumber(108);
notificationBuilder.setSmallIcon(R.drawable.icon);
notificationBuilder.setOngoing(true);
notificationBuilder.setPriority(5);
Notification notification = notificationBuilder.build();
NotificationManager notificationManger =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManger.notify(1, notification);
try {
startForeground(3, notification);
}catch (Exception e) {
e.printStackTrace();
}
super.onStartCommand(intent, flags, startId);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(getApplicationContext(), MyService.class));
} else {
startService(new Intent(getApplicationContext(), MyService.class));
}
registerReceiver(restarter,restarter.getFilter());
//work
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("restartservice");
broadcastIntent.setClass(this, Restarter.class);
this.sendBroadcast(broadcastIntent);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论