2017-11-06 15 views
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メソッドの開始時にそれを呼び出すことを知って、私のために動作しません。

答えて

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()); 
    } 
  • ... MainActivityで怒鳴るのコードを追加します。

    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 
         } 
    
    
        } 
    
        } 
    
    } 
    
  • ScreenOnOffReceiverクラス&ペースト怒鳴るコードを作成します

    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

  • 関連する問題