3
안녕하세요, 저는 Android가 처음인데 화면이 꺼지면 앱을 죽이고 싶습니다. 화면이 시간 초과되거나 잠금 버튼을 누르는 경우를 의미합니다. 나는이 방법을 시도했다 그러나 나는 불행하게도이 나를 위해 작동하지 않습니다 내 MainActivity화면이 꺼질 때마다 앱을 종료하십시오.
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);
}
에하여 onCreate
방법의 시작 부분에 호출 것을 알고, 나를 위해 작동하지 않습니다.