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

发布于 2024-12-27 06:34:44 字数 2208 浏览 0 评论 0原文

当用户触摸屏幕时,我的应用程序正在完成。为此,在 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)

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

提前致谢。

My app is finishing when user touch the screen. For this, on onTouch() method I have

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

where FinActivity class is this one :

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();
    }

I want to restart my app when screen is OFF. I have this AlarmReceiver class :

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;
        }
    }

}

but after 60 seconds I get NullPointerException at this line : intent.getAction().equals(Intent.ACTION_SCREEN_OFF)

Where is my mistake ? What i do wrong?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路终见情 2025-01-03 06:34:44

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

您可以获取更多详细信息 http://developer.android.com/reference/android/ os/PowerManager.html 此处。

If you just want to know whether your screen is turned on or off, you can use PowerManager class of android it is from api level 1.You can use isScreenOn() method for knowing status of screen.

You can get more details http://developer.android.com/reference/android/os/PowerManager.html here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文