服务在关闭应用程序后约30-40分钟被系统停止/终止,尽管它是前台服务。

huangapple 未分类评论42阅读模式
英文:

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;
}
}

huangapple
  • 本文由 发表于 2020年4月5日 15:55:52
  • 转载请务必保留本文链接:https://java.coder-hub.com/61039623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定