小编Mad*_*ina的帖子

前台服务不会一直运行

在我的应用程序中,我使用必须不断运行的前台服务。有时前台服务会停止。在什么情况下操作系统会终止我的服务(即使内存足够,电池已满,手机正在充电也会发生)?

这是我的代码到目前为止的样子:

public class ServiceTest extends Service {

    public static Thread serverThread = null;
    public Context context = this;

    public ServiceTest(Context context) {
        super();
        this.context = context;
    }

    public ServiceTest() {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        if (this.serverThread == null) {
            this.serverThread = new Thread(new ThreadTest());
            this.serverThread.start();
        }

        return START_STICKY;
    }

    private class ThreadTest implements Runnable {

        @Override
        public void run() {
            Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); …
Run Code Online (Sandbox Code Playgroud)

android foreground-service

2
推荐指数
1
解决办法
3439
查看次数

标签 统计

android ×1

foreground-service ×1