Android 在屏幕关闭时重新启动应用程序

Android 在屏幕关闭时重新启动应用程序,android,screen,broadcastreceiver,alarm,Android,Screen,Broadcastreceiver,Alarm,当用户触摸屏幕时,我的应用程序即将完成。对于这一点,我使用了onTouch()方法 Intent intent = new Intent(getBaseContext(), FinActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); 其中FinActivity类是这一类: public class FinActivity extends Activit

当用户触摸屏幕时,我的应用程序即将完成。对于这一点,我使用了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\u SCREEN\u OFF)

我的错在哪里?我做错了什么


提前感谢。

如果您只想知道屏幕是打开还是关闭的,您可以使用android的PowerManager类,它来自api级别1。您可以使用IsCreenon()方法了解屏幕状态


您可以在此处获得更多详细信息。

如果检查空a
Intent
并忽略它,是否有效?它会在没有if条件的情况下重新启动应用程序。