防止前台服务进入睡眠模式

时间:2020-02-22 10:15:51

标签: android sleep-mode

我有一个简单的前台服务,该服务应该每秒记录一次文件,并使其隔夜运行。 在应用程序刚刚启动时,它运行良好,并且我每秒都有日志记录:

Fri Feb 21 2020 at 11:35:12:809 pm  startThread: 4371
Fri Feb 21 2020 at 11:35:13:814 pm  startThread: 4372
Fri Feb 21 2020 at 11:35:14:818 pm  startThread: 4373
Fri Feb 21 2020 at 11:35:15:819 pm  startThread: 4374
Fri Feb 21 2020 at 11:35:16:822 pm  startThread: 4375
Fri Feb 21 2020 at 11:35:17:830 pm  startThread: 4376
Fri Feb 21 2020 at 11:35:18:836 pm  startThread: 4377
Fri Feb 21 2020 at 11:35:19:841 pm  startThread: 4378

以后的电话没有使用很长时间,我想它进入了睡眠模式。现在我的日志文件中有时间间隔:

Sat Feb 22 2020 at 02:00:01:581 am startThread: 6232
Sat Feb 22 2020 at 02:00:06:067 am startThread: 6233
Sat Feb 22 2020 at 02:00:07:076 am startThread: 6234
Sat Feb 22 2020 at 02:01:00:741 am startThread: 6235
Sat Feb 22 2020 at 02:01:16:334 am startThread: 6236
Sat Feb 22 2020 at 02:01:17:340 am startThread: 6237
Sat Feb 22 2020 at 02:01:21:338 am startThread: 6238
Sat Feb 22 2020 at 02:09:59:062 am startThread: 6239
Sat Feb 22 2020 at 02:10:00:069 am startThread: 6240
Sat Feb 22 2020 at 02:12:15:060 am startThread: 6241
Sat Feb 22 2020 at 02:12:16:076 am startThread: 6242

即使手机处于睡眠模式,是否有可能以某种方式要求Android继续计算任务?如果入睡,是否可以以某种方式唤醒电话?

public class ExampleService extends Service {

    class ExampleThread extends Thread {
        int seconds;
        ExampleThread(int seconds) {
            this.seconds = seconds;
        }

        @Override
        public void run() {
            for (int i = 0; i < seconds; i++) {
                Timber.tag(Utils.TIMBER_TAG).v("startThread: " + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Example Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_android)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);

        //do heavy work on a background thread
        //stopSelf();
        longJob();
        return START_STICKY;
    }


    public void longJob()
    {
        ExampleThread thread = new ExampleThread(60*60*2);
        thread.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Timber.tag(Utils.TIMBER_TAG).v("Destroy");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

通过活动呼叫此服务器:

    Intent serviceIntent = new Intent(this, ExampleService.class);
    serviceIntent.putExtra("inputExtra", "data");
    ContextCompat.startForegroundService(this, serviceIntent);

1 个答案:

答案 0 :(得分:0)

Enable wakelock在您的服务onStartCommand中,然后调用longJob();

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp::MyWakelockTag");
wakeLock.acquire();

完成任务/执行后,您可以调用以下命令轻松释放唤醒锁:wakelock.release()

*请注意, WakeLock 可以防止CPU进入睡眠状态,从而可能导致过多的热量和电池消耗。