There is usually a scenario where you share content to wechat moments, etc., and then click a button in the content to invoke your own app.
If you want to wake up the App, the App must register a global event listener first. Then, there should be a page to handle the accept event and then parse out the specific parameters and jump to the specific page. It’s that simple.
Get your ideas straight, so let’s do it one by one.
Register event Listeners
is used in the Android Activity. Now you can create an Activity that resolves the jump by naming it whatever you want. Then you need to configure the specific
in the Manifest file.
<intent-filter>
<data
android:scheme="test"
android:host="lovejjfg.com"
/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
Copy the code
The Activity now has the ability to wake up externally. Note the configuration in , and the external link should look like this: test://lovejjfg.com/xxx. There are other things that can be defined in , which I won’t expand on here.
Page jump
Let’s say you have A startup page A, home page B, and now you want to jump to page C. So when external App is invoked, there are actually several situations that we need to consider.
First consideration, this depends on the requirements of the product, that is to open the specific page, whether to just launch the page, back to the browser directly, or need to launch the App. If you just open the page, you can simply jump to the page, regardless of the new task stack. If the rollback is to launch the target App, or the user goes straight back to the browser, there are a few more cases to distinguish.
The first case is that the target App has not been started in the current mobile phone. Simply put, the browser jumps directly to page C, and when it resets, it displays page A and then goes to page B. Here we need to create A stack ourselves, put A and C in order, so that C falls back to A, and A can start B. The knowledge point is the TaskStackBuilder, which is complemented by the Parent attribute that can be specified in the Manifest.
The class name of the Activity’s logical parent. The name here must be the same as the class name specified for the Android: Name attribute of the corresponding element. This property is read to determine which Activity should be started when the user presses the Up button in the action bar. The system can also use this information to compose the Activity’s return stack through TaskStackBuilder. To support API levels 4-16, you can also declare a parent Activity using an element with a value of “Android.support. PARENT_ACTIVITY”.
So it’s up to you to fit in. Let’s look at the TaskStackBuilder. It’s not that hard to copy. Ha ha. PendingIntent () = startActivity(); startActivity() = startActivity()
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(resultIntent.getComponent());
stackBuilder.addNextIntent(resultIntent);
stackBuilder.startActivities();
Copy the code
In fact, I did not know how to start the Activity directly at first, because there is no such writing in the copy, so I have to go to see the method. Stackbuilder.getintents () is used to start the Activity on stackBuilder.getintents (). There is more on stackBuilder.getintents () than on stackBuilder.startActivities (). That’s exactly the way I wrote it.
Wait, why did I write Builder like that? Isn’t that an insult to the Builder model?
TaskStackBuilder.create(this)
.addParentStack(resultIntent.getComponent())
.addNextIntent(resultIntent)
.startActivities();
Copy the code
That’s the right way to do it. In the second case, the target App is already up, running in the background, and the specified C page is not open. The above way, whether your App is started or not, it will restart, this is also a bit annoying, so why does it restart every time? Just look at the startup method.
public void startActivities(Bundle options) {
if (mIntents.isEmpty()) {
throw new IllegalStateException(
"No intents added to TaskStackBuilder; cannot startActivities");
}
Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]);
intents[0] = new Intent(intents[0]).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK |
IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME);
if (!ContextCompat.startActivities(mSourceContext, intents, options)) {
Intent topIntent = new Intent(intents[intents.length - 1]);
topIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mSourceContext.startActivity(topIntent);
}
}
Copy the code
The key point, This method will add the Intent to the first Intent. Each FLAG_ACTIVITY_NEW_TASK | IntentCompat. FLAG_ACTIVITY_CLEAR_TASK | Flagentcompat.FLAG_ACTIVITY_TASK_ON_HOME: IntentCompat.FLAG_ACTIVITY_CLEAR_TASK: IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME: IntentCompat.FLAG_ACTIVITY_CLEAR_TASK: IntentCompat. In fact, it is very simple, we first determine whether the current App has been opened, if not, then directly above the code, if there is, then do not need to create a stack, directly open it. FLAG_ACTIVITY_NEW_TASK: Intents.FLAG_ACTIVITY_NEW_TASK: intents.FLAG_ACTIVITY_NEW_TASK: intents.
if (ViewUtils.isLaunchedActivity(this, HomeActivity.class)) { resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(resultIntent); } else { TaskStackBuilder.create(this) .addParentStack(resultIntent.getComponent()) .addNextIntent(resultIntent) .startActivities(); } public static boolean isLaunchedActivity(@NonNull Context context, Class<? > clazz) { Intent intent = new Intent(context, clazz); ComponentName cmpName = intent.resolveActivity(context.getPackageManager()); boolean flag = false; if (cmpName ! = null) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskInfoList = am.getRunningTasks(10); for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) { if (taskInfo.baseActivity.equals(cmpName)) { flag = true; break; } } } return flag; }Copy the code
In the third case, the target App is already started, running in the background, and the specified C page is open.
In fact, this is the problem of the startup mode, C has been opened, opened again, if it is a serious startup mode, there must be multiple C pages repeatedly, so, set a SingleTop is to solve the problem. Of course, if this mode is set, you need to deal with the onNewIntent() method.
Argument parsing
Test ://lovejjfg.com/C?10086
Uri data = getIntent().getData(); String host = data.getHost(); String path = data.getPath(); String id = data.getQueryParameter("id") String scheme = data.getScheme(); Log.i(TAG, "host: " + host); //lovejjfg.com Log.i(TAG, "path: " + path); //C Log.i(TAG, "scheme: " + scheme); //test Log.i(TAG, "id: " + id); / / '10086'Copy the code
The idea above is not limited to the use of Scheme jumps, but the same is true for Notification. And is startActivities() pretty cool?
PS: If you have nothing to do, read more official documents. Many of them have been Translated into Chinese.
-2017 12 28 Update –
Since there are many other partners with similar needs, some of these issues have been addressed in the comments. Add test Demo and test link, add notification jump, convenient for everyone to test.
Address: github.com/lovejjfg/Ea…