如何在收到通知时唤醒屏幕?

时间:2017-07-31 12:19:26

标签: java android

对于我的应用程序,我试图将通知唤醒屏幕并显示应用程序中的视图。我无法想象如何让应用程序在锁定屏幕时唤醒。我尝试过一些东西,但似乎都没有用,或者它们使应用程序崩溃。

感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

此BroadCastReceiver适用于,您的应用程序处于后台状态/移动状态锁定模式当我收到通知时,我必须重定向特定屏幕,为此我添加了Intent代码, 收到通知后,此代码有助于您的要求

公共类FirebaseDataReceiver扩展了WakefulBroadcastReceiver {

  private final String TAG = "FirebaseDataReceiver";

    public void onReceive(Context context, Intent intent) {
        PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
        boolean isScreenOn = pm.isScreenOn();
        if(isScreenOn==false)
        {
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
            wl.acquire(10000);
            PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");

            wl_cpu.acquire(10000);
        }
       //Redirect particular screen after receiving notification, this is like ola driver app concept accepting driver request
        intent = new Intent(context, MyTicketListActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

    }
}

抱歉我的英文

答案 1 :(得分:2)

有我的解决方案:

createNotification(); //your implementation
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = Build.VERSION.SDK_INT >= 20 ? pm.isInteractive() : pm.isScreenOn(); // check if screen is on
if (!isScreenOn) {
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "myApp:notificationLock");
    wl.acquire(3000); //set your time in milliseconds
}

更多{@ {3}}

答案 2 :(得分:0)

我也遇到过类似的情况。需要显示通知屏幕,以便用户接受或拒绝通知,即使在屏幕关闭时也是如此。我摸索了一段时间直到现在。要求要求屏幕应该打开,这可以通过窗口管理器和唤醒锁的标志组合来实现,如下所示。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
    boolean isScreenOn = pm.isScreenOn();
    int flags = PowerManager.FULL_WAKE_LOCK
            | PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.ON_AFTER_RELEASE;
    if (!isScreenOn) {
        wakeLock = pm.newWakeLock(flags, "my_app:full_lock");
        wakeLock.acquire(20000);
    }

    setContentView(R.layout.activity_incoming_request);
    
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
            WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
    ...
    startRingingPhone();
}

此代码块将在锁定屏幕上显示活动。