【问题标题】:Background service stops when phone goes in idle state当手机进入空闲状态时后台服务停止
【发布时间】:2017-05-24 14:48:14
【问题描述】:

我有一个必须一直运行的粘性后台服务。基本上它跟踪时间,它是一个自定义时钟计时器。但是一旦设备进入空闲状态(当手机屏幕关闭时),计时器(后台服务)也会暂停。

我应该怎么做才能让它在屏幕关闭的情况下始终保持运行?

    public class TimerService extends Service {
    private Context context;
    @Override
        public IBinder onBind(Intent intent) {
            Logger.LogI(TAG, "Service binding");
            return null;
        }

      @Override
        public void onCreate() {
            super.onCreate();
            context = getApplicationContext();
        }

     @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
       return Service.START_STICKY;
        }
    }

【问题讨论】:

  • 能否请您发布您的代码..
  • 您的清单有wakeLock 权限吗?
  • 是的,我有。但它会耗尽电池。我正在寻找优化的解决方案。但也可以随意分享该解决方案。因为服务需要一直运行。

标签: android performance service


【解决方案1】:

因此,当设备进入睡眠模式时,有一些方法可以防止服务被终止。 我将服务作为附加通知的前台服务启动。我并不是说它是长期服务的更好方法。但是当然,这个解决方案是开放的,可以进行更多优化。此解决方案不会让服务在睡眠模式下进入暂停状态。

以下是整个服务代码:

public class TimerService extends Service {
    private ScheduledExecutorService scheduleTaskExecutor;
    private Context context;
    @Override
    public IBinder onBind(Intent intent) {
        Logger.LogI(TAG, "Service binding");
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        setNotification();
        if (scheduleTaskExecutor == null) {
            scheduleTaskExecutor = Executors.newScheduledThreadPool(1);
            scheduleTaskExecutor.scheduleAtFixedRate(new mainTask(), 0, 1, TimeUnit.SECONDS);
        }
        return Service.START_STICKY;
    }

    public void setNotification() {
        PendingIntent contentIntent;
        contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, Main_Activity.class), 0);

        NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setColor(this.getResources().getColor(R.color.action_bar_color))
            .setContentTitle("MainActivity")
            .setOngoing(true)
            .setAutoCancel(false)
            .setContentText("MyApp");
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

        Notification notification = mBuilder.build();
        startForeground(NOTIFICATION_ID, notification); //NOTIFICATION_ID is a random integer value which has to be unique in an app
    }
}
private class mainTask implements Runnable {

    public void run() {
        // 1 Second Timer
    }
}

有用的链接:

Run a service in the background forever..? Android

How to run background service after every 5 sec not working in android 5.1?

How to run a method every X seconds

How to make service run even in sleep mode?

part-1 persistent foreGround android service that starts by UI, works at sleep mode too, also starts at phone restart

part-2 persistent foreGround android service that starts by UI, works at sleep mode too, also starts at phone restart

Android - implementing startForeground for a service?

【讨论】:

    【解决方案2】:

    如果您想让您的服务成为永无止境的服务,请在 onStartCommand 中使用此代码

    @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
         //Put code here
        return START_REDELIVER_INTENT;
    
    }
    

    您也可以使用 START_STICKY。

    【讨论】:

    • 让我再次重现此问题,我会尽快回复您。
    • AhmadShahwaiz:你检查过这个吗?
    • 这个案例并没有在我已经写好的代码中重现。我已经在使用 Start_Sticky 服务。 this 和 start_redeliver_intent 的主要区别在于,在 start_redeliver_intent 服务中,服务重新启动时会再次发送先前的意图。我看不到 start_redeliver_intent 进入空闲状态时如何暂停服务。我会等待它重现,然后我会使用你的解决方案,如果它有效,然后我会标记你的答案。
    • 因此,当我“分离”为调试而连接的电线时,该案例再现了。你的解决方案没有奏效。当手机进入睡眠模式时,服务仍然会暂停。首先,它会关闭 CPU。在此过程中,非必要的无线电(WiFi、GPS)也将被关闭。
    猜你喜欢
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 2011-09-12
    相关资源
    最近更新 更多