Preface
I have encountered a very strange problem recently and finally solved it, so I thought about recording it so that everyone or you can refer to it when you need it in the future.
Problem scenario
Use Xiaomi to push a message using Xiaomi, then click the message in the notification bar to launch the application, and then enter the session's activity. After the application is started, if the current interface is not a session interface, a new message will display a message reminder in the notification bar, and then click on the session message but cannot enter the session activity. That is, after clicking the notification bar, the system does not intend to start the specified activity, and does not see the system starts the activity log, and then you will see the shadow of the system processing this activity.
This specified activity is not the activity of the session, but is specified in the fileActivity A. In other words, if there is a session message, you will start with this A first, and then pass the data to the activity behind it.
There are two ways to display notifications here. One is popped up by the mobile phone system in the notification bar. For example, Xiaomi pushes it on Xiaomi mobile phone, Huawei pushes it on Huawei mobile phone, and the other is popped up by the remote process of the application.
There are two ways to start the first Activity A of the application. One is to directly construct an Intent through new, and then pass it into the class of Activity A; the other is to().getLaunchIntentForPackage(())
to get the Intent of Activity A started. Then call()
Method, pass the obtained intent in.
Then the problem is. If you click the notification bar popped up by the system or the notification bar popped up by the remote process, and if you just use one of the startup methods to start the application, then after the application is started, click the new message notification popped up by the remote process in the background in the notification bar, and you cannot enter the activity of the session. Judging from the system logs, the Activity was not started, but the Activity was only processed.
Some people may wonder if they want to add oneIntent.FLAG_ACTIVITY_NEW_TASK
Identify becausegetLaunchIntentForPackage()
This logo is added to the method.
Finally, the test found that as long as the application is not started, whether it is clicking on the notification bar popped up by the system or the notification bar popped up by the remote process, if you receive a new message notification and click on the notification bar, you can enter the session activity. Then it is OK as long as you judge whether an Activity in the application is started, it seems that the problem can be solved.
Problem solving
So the following logic is used to determine whether the foreground activity is running.
/** * Determine whether the UI process is running * @return Return true means running, otherwise it is not running */ public static boolean isForegroundRunning() { ActivityManager am = (ActivityManager) ().getSystemService(Context.ACTIVITY_SERVICE); List<> list = (); if (list != null) { for ( info : list) { if ( == .IMPORTANCE_FOREGROUND && ().getPackageName().equals()) { return true; } } } return false; }
expand
However, the above method works on Xiaomi phones, but there are still problems on Huawei phones, even in the same scenario. Huawei is cheating again!
So I started from aboveStart with the status of the importance variable in the class, and then test the variable values that may appear in various scenarios. The results were found to be unsatisfactory, and some scenarios still have problems.
Finally, another way of thinking: do not start the application from Activity A, change Activity B, that is, calling()
The method passes into the Intent object using B's class. Starting B will find that the application has not been initialized, so jump to A to perform initialization, and then go to normal process.
Then test the various scenarios and various models to find and solve the problem. From the above, we can see that although we don’t understand the principle behind it, we must have broad ideas for solving the problem, especially when we are in a hurry to release the version, don’t hang yourself on a tree.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.