2012-01-16 12 views
2

画面にタッチするとアプリが終了します。このため、onTouchに()メソッドは、私が画面がオフのときにアプリを再起動

Intent intent = new Intent(getBaseContext(), FinActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent); 
finish(); 

FinActivityクラスはこの1つですした場所:

public class FinActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

     IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
     filter.addAction(Intent.ACTION_SCREEN_OFF); 
     BroadcastReceiver mReceiver = new AlarmReceiver(); 
     registerReceiver(mReceiver, filter); 

     Intent intent = new Intent(this, AlarmReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(
       this.getApplicationContext(), 234324243, intent, 0); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
       System.currentTimeMillis() + (60 * 1000), 
       System.currentTimeMillis() + (60 * 1000), pendingIntent); 
     finish(); 
    } 

私は、画面がオフの時に私のアプリを再起動します。私はこのAlarmReceiverクラスを持っている:

public class AlarmReceiver extends BroadcastReceiver { 

    public static boolean wasScreenOn = true; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
      // do whatever you need to do here 
      System.out.println("Screen OFF"); 
      wasScreenOn = false; 

       Intent i = new Intent(context, SplashScreen.class); 
     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(i); 

     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
      // and do whatever you need to do here 
      System.out.println("Screen ONN"); 
      wasScreenOn = true; 
     } 
    } 

} 

が、60秒後に、私はこのラインでNullPointerExceptionが取得:私のミスですintent.getAction().equals(Intent.ACTION_SCREEN_OFF)

?私は間違っていますか?

ありがとうございます。

+0

「Intent」を確認して無視すると機能しますか? – Glitch

+0

if条件なしでアプリを再起動します。 – Gabrielle

答えて

2

あなたは自分の画面がオンまたはオフにされているかどうかを知りたい場合は、それは。あなたがの状態を知るためisScreenOn()メソッドを使用することができますレベル1APIからであるアンドロイドのPowerManagerクラスを使用することができます画面。

詳細はこちらhttp://developer.android.com/reference/android/os/PowerManager.htmlをご覧ください。

関連する問題