I also had this requirement in one of my applications. We can achieve this if we call
Intent mIntent = new Intent(context, MainActivity.class); mIntent.setAction(Intent.ACTION_MAIN); mIntent.addCategory(Intent.CATEGORY_LAUNCHER); mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(mIntent); broadcastMessage(context, message);
In the main action, use the following in the broadcast receiver, which will receive the transmitted message above.
WakeLock wakeLock = null; KeyguardManager kgMgr = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); boolean locked = kgMgr.inKeyguardRestrictedInputMode(); PowerManager pm = (PowerManager) context .getSystemService(Context.POWER_SERVICE); if (!pm.isScreenOn()) { wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MyWakeLock"); wakeLock.acquire(); } if (locked) { Window mWindow = getWindow(); mWindow.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); mWindow.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); }
Personally, I feel that this is not the best answer, as well as the best ideas to open the application directly when you receive a notification, since there will be many functions, such as onCreate onResume, that will automatically start, ruining the work of users if they are in a really important job When opening another application directly, we also need to set many flags or use any other method to control the flow of the application, when the user opens the application, the application comes from the background, the application opens from and notice all such cases. Avoid it as it spoils the entire user experience.
source share