2012-01-16 3 views
2

사용자가 화면을 터치하면 앱이 종료됩니다. 이를 위해, onTouch() 메소드 나는 FinActivity 클래스는이 하나입니다화면이 꺼져있을 때 앱 다시 시작

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

있습니다

나는 화면이 꺼져있을 때 내 응용 프로그램을 다시 시작하겠습니다
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; 
     } 
    } 

} 

하지만이 줄에서 NullPointerException이 얻을 60 초 동안 내 실수 intent.getAction().equals(Intent.ACTION_SCREEN_OFF)

입니까? 내가 뭘 잘못 했니?

미리 감사드립니다.

+0

null을 '인 텐트'로 확인하고 무시하면 제대로 작동합니까? – Glitch

+0

if 조건없이 앱을 다시 시작하십시오. – Gabrielle

답변

2

당신은 당신의 화면을 설정 또는 해제 여부를 알고 싶은 경우에, 당신은 우선은의 상태를 알고에 대한 isScreenOn() 방법을 사용할 수 있습니다 레벨 1API에서 안드로이드의 PowerManager 클래스을 사용할 수 있습니다 화면.

자세한 내용은 http://developer.android.com/reference/android/os/PowerManager.html 여기에서 확인할 수 있습니다.

관련 문제