gpt4 book ai didi

android - 在 Android 中解锁设备屏幕时启动服务

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:46 24 4
gpt4 key购买 nike

我想在屏幕解锁时运行我的服务,并在屏幕锁定时停止它。我调查了these answers并实现了它们。 但是,当我锁定屏幕时,服务会按要求停止,但当我解锁屏幕时,它不会再次启动。

运行该服务的代码如下:

public class PhonePositionService extends Service {
@Override
public void onCreate() {
//ADDED CODE
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new BootCompletedIntentReceiver();
registerReceiver(mReceiver, filter);
{...}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int icon = R.drawable.ic_stat_bright;
startForeground(NOTIFICATION_ID, getCompatNotification(icon));
if (backgroundThread != null) {
backgroundThread.start();
}
return START_STICKY;
}
}

在其他文件中,我的广播接收器代码如下:

public class BootCompletedIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_SCREEN_ON.equals(action)) {
// start the service
Intent pushIntent = new Intent(context, PhonePositionService.class);
context.startService(pushIntent);
} else if(Intent.ACTION_SCREEN_OFF.equals(action)) {
// stop the service
Intent pushIntent = new Intent(context, PhonePositionService.class);
context.stopService(pushIntent);
}
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, PhonePositionService.class);
context.startService(pushIntent);
}
}
}

那为什么当我解锁手机时服务没有重新启动?

最佳答案

首先,在您拥有管理员权限之前,无法检测用户是否已解锁手机。您面临的问题是因为您在屏幕关闭时使用 stopService() 停止了 PhonePositionService:

public class BootCompletedIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_SCREEN_ON.equals(action)) {
// start the service
Intent pushIntent = new Intent(context, PhonePositionService.class);
context.startService(pushIntent);
} else if(Intent.ACTION_SCREEN_OFF.equals(action)) {
// stop the service
Intent pushIntent = new Intent(context, PhonePositionService.class);
//This will stop the service
context.stopService(pushIntent);
}
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, PhonePositionService.class);
context.startService(pushIntent);
}
}
}

这会导致一些内存泄漏,因为 BroadcastReceiver 没有取消注册。

不鼓励让服务永远在前台运行,因为这可能导致(快速)电池耗尽。

我建议您寻找其他替代方案。但是,如果您的核心功能受到影响但您仍想继续,则可以执行以下变通办法:

BootCompletedIntentReceiver 作为 PhonePositionService 的内部类。您无需启动和停止服务,而是直接执行您的操作。

public class PhonePositionService extends Service {
@Override
public void onCreate() {
//ADDED CODE
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new BootCompletedIntentReceiver();
registerReceiver(mReceiver, filter);
...
}
...
private class BootCompletedIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_SCREEN_ON.equals(action)) {
//DO action for SCREEN_ON
} else if(Intent.ACTION_SCREEN_OFF.equals(action)) {
//Do action for SCREEN_OFF
}

}
}
}

关于android - 在 Android 中解锁设备屏幕时启动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49746266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com