Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当手机处于空闲状态时,后台服务停止_Android_Performance_Service - Fatal编程技术网

Android 当手机处于空闲状态时,后台服务停止

Android 当手机处于空闲状态时,后台服务停止,android,performance,service,Android,Performance,Service,我有一个粘性后台服务,它必须一直运行。基本上,它跟踪时间,这是一个定制的时钟计时器。但一旦设备进入空闲状态(当手机屏幕关闭时),计时器(后台服务)也会暂停 我该怎么做才能使它即使在屏幕关闭时也能一直运行 public class TimerService extends Service { private Context context; @Override public IBinder onBind(Intent intent) {

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

我该怎么做才能使它即使在屏幕关闭时也能一直运行

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

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

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

}

您也可以使用START_STICKY。

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

以下是整个服务代码:

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
    }
}
有用链接:


你能发布你的代码吗..你在你的舱单上有
wakeLock
权限吗?是的,我有。但这会耗尽电池电量。我正在寻找优化的解决方案。但也可以随意分享这个解决方案。因为这项服务需要一直运行。让我再重复一遍这个问题,然后再给你回复。艾哈迈德沙瓦伊兹:你检查过了吗?这个案例没有在我已经编写的代码中重复。我已经在使用Start\u粘性服务。此意图与start_redeliver_意图的主要区别在于,在start_redeliver_意图服务中,当服务重新启动时,会再次发送上一个意图。我不知道start\u redeliver\u intent如何在服务处于空闲状态时暂停服务。我将等待它重现,然后我将使用您的解决方案,如果它有效,那么我将标记您的答案。因此,当我“分离”连接用于调试的电线时,案例重现。你的解决方案不起作用。当手机进入睡眠模式时,服务仍会暂停。它主要关闭CPU。一路上,非必要的无线电(WiFi、GPS)也将被关闭。