收到通知后如何使屏幕唤醒?

Chr*_*zie 6 java android

对于我的应用程序,我试图在通知唤醒屏幕并显示该应用程序视图的位置获取它。我不知道如何在锁定屏幕上唤醒应用程序。我已经尝试了一些方法,但似乎没有任何效果,或者它们使应用程序崩溃。

任何帮助表示赞赏。

小智 5

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

public class FirebaseDataReceiver extends 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);
    
        }
    }
Run Code Online (Sandbox Code Playgroud)

还记得指定在 中使用的权限AndroidManifest.xml

public class FirebaseDataReceiver extends 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);
    
        }
    }
Run Code Online (Sandbox Code Playgroud)


Jan*_*vec 5

有我的解决方案:

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
}
Run Code Online (Sandbox Code Playgroud)

PowerManager的更多内容