当用户触摸屏幕时,我的应用即将完成。为此,onTouch()方法我有
Intent intent = new Intent(getBaseContext(), FinActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
其中FinActivity类就是这个:
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 :(得分:2)
如果你只是想知道你的屏幕是打开还是关闭,你可以使用Android的 PowerManager类来自 api level 1 。你可以使用< strong> isScreenOn()了解屏幕状态的方法。
您可以在此处获取更多详细信息http://developer.android.com/reference/android/os/PowerManager.html。