画面のロックが解除されたらサービスを実行し、画面がロックされたらサービスを停止したいと思います。私はこれらの答えを調べて実装しました。ただし、画面をロックすると必要に応じてサービスが停止しますが、画面のロックを解除してもサービスは再開されません。
このサービスを実行するためのコードは次のとおりです。
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);
}
}
}
それでは、電話のロックを解除したときにサービスが再開されないのはなぜですか?
まず第一に、管理者権限を取得するまで、ユーザーが電話のロックを解除したかどうかを検出することはできません。あなたが直面している問題は、画面がオフのときにPhonePositionService
使用を停止しているためですstopService()
。
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
}
}
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加