2017-11-06 3 views
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 방법의 시작 부분에 호출 것을 알고, 나를 위해 작동하지 않습니다.

답변

1

당신은

ScreenOnOffReceiver sonoff; 
    try { 
     //Log.e("Error Welcome :","Welcome Begin"); 
     sonoff = new ScreenOnOffReceiver(); 
     IntentFilter intentFilter = new IntentFilter(); 
     intentFilter.addAction(Intent.ACTION_SCREEN_ON); 
     intentFilter.addAction(Intent.ACTION_SCREEN_OFF); 
     registerReceiver(sonoff, intentFilter); 
     //Log.e("Screen On Off ", "Started"); 
    } catch (Exception e) { 
     //Log.e("Error Welcome :", e.toString()); 
    } 
  • 에 우는 ​​코드를 추가 ... ScreenOnOffReceiver 클래스 & 붙여 넣기를 넣고 코드

    public class ScreenOnOffReceiver extends BroadcastReceiver{ 
    
    @Override 
        public void onReceive(final Context context, Intent intent) { 
    
        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
        { 
        // Log.e("Sleep Time", getTimeForSleep(context)+""); 
    
        } 
    
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
        { 
         // some code 
         Log.e("screen-- ", "off"); 
    
         try { 
         Intent intent1 = new Intent(context, MainActivity.class); 
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         context.startActivity(intent); 
         ((Activity)context).finish(); 
    
         // sleepTime = getTimeForSleep(context); 
    
         } catch (Exception e) { 
         // TODO: handle exception 
         } 
    
    
        } 
    
        } 
    
    } 
    
  • MainActivity에서 울부 짖는 코드를 추가 만들기

    1. 을 시도 할 수 있습니다 AndroidManifest.xml

      <receiver android:name=".ScreenOnOffReceiver"> 
           <intent-filter> 
            <action android:name="android.intent.action.SCREEN_ON" /> 
            <action android:name="android.intent.action.SCREEN_OFF" /> 
           </intent-filter> 
          </receiver> 
      
    2. 귀하의 문제는 해결 될 것입니다. 사전 감사 ... Enamul

  • 관련 문제