3
こんにちは、私はアンドロイドの世界にはいないし、スクリーンが消えたときにアプリを殺したいと思っています。画面のタイムアウトやロックボタンが押されたときです。 私はこの方法を試してみたが、それは、私は残念ながら、これは私のために動作していません画面が消えるたびにアプリを終了する
private void registerBroadcastReceiver() {
final IntentFilter theFilter = new IntentFilter();
/** System Defined Broadcast */
theFilter.addAction(Intent.ACTION_SCREEN_ON);
theFilter.addAction(Intent.ACTION_SCREEN_OFF);
theFilter.addAction(Intent.ACTION_USER_PRESENT);
BroadcastReceiver screenOnOffReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String strAction = intent.getAction();
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
assert strAction != null;
if(strAction.equals(Intent.ACTION_USER_PRESENT) || strAction.equals(Intent.ACTION_SCREEN_OFF) || strAction.equals(Intent.ACTION_SCREEN_ON) ) {
assert myKM != null;
if(myKM.inKeyguardRestrictedInputMode())
{
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);;
} else
{
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
}
}
}
};
getApplicationContext().registerReceiver(screenOnOffReceiver, theFilter);
}
私MainActivityにonCreate
メソッドの開始時にそれを呼び出すことを知って、私のために動作しません。