如何在屏幕关闭时打开应用程序

How to open application when screen is off

我想在收到推送通知时打开我的应用程序。现在,收到推送时应用程序未打开。这是我正在使用的代码,

添加了 WAKE_LOCK 清单权限,例如

<uses-permission android:name="android.permission.WAKE_LOCK" />

这是我打开应用程序的代码,

 Intent intent = new Intent(this, HomeActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 intent.putExtra(Constants.NewOrderRequest, true);
 startActivity(intent);

希望这可以帮助you.But你必须在服务中完成。

private ScreenObserver screenObserver;
public void onCreate() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_SCREEN_ON);
    screenObserver = new ScreenObserver();

    registerReceiver(screenObserver, filter);
}
private class ScreenObserver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                if (isDebug) Log.d(TAG, "ScreenON");
                Intent intent = new Intent(this, HomeActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Constants.NewOrderRequest, true);
                startActivity(intent);
            }
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                if (isDebug) Log.d(TAG, "ScreenOFF");
            }
        }
    }

使用它从服务

触发activity
Intent inte = new Intent(context, Activity.class);


                    inte.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    inte.addCategory(intent.CATEGORY_LAUNCHER);
                    context.startActivity(inte);

并在 activity 中将其置于 onCreate

   Window window=this.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);