Android 防止前台服务进入睡眠模式
我有一个简单的前台服务,它应该每秒钟登录一次文件,然后让它通宵运行。 当应用程序刚刚启动时,它工作正常,我有每秒的日志记录:Android 防止前台服务进入睡眠模式,android,sleep-mode,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:
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;
}
}
在调用longJob()之前,在服务的onStartCommand
中代码>
完成任务/执行后,可以通过调用:wakelock.release()
*请注意,WakeLock会阻止CPU睡眠,从而导致过热和电池放电
Intent serviceIntent = new Intent(this, ExampleService.class);
serviceIntent.putExtra("inputExtra", "data");
ContextCompat.startForegroundService(this, serviceIntent);
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp::MyWakelockTag");
wakeLock.acquire();