DeepLink usage and source code parsing (instance description), deeplink instance description
1. Introduction
DeepLink has the following explanation on its official website:
When a clicked link or programmatic request invokes a web URI intent, the Android system tries each of the following actions, in sequential order, until the request succeeds:1. Open the user's preferred app that can handle the URI, if one is designated.2. Open the only available app that can handle the URI.3. Allow the user to select an app from a dialog.Follow the steps below to create and test links to your content. You can also use the [App Links Assistant](https://developer.android.com/studio/write/app-link-indexing.html) in Android Studio to add Android App Links
After translation, it means:
When you click a link or program request to call the Web URI intent, the Android system tries the following operations in sequence until the request is successful:
1. Open your preferred application, which can process the URI, if specified.
2. Open the only available application that can process the URI.
3. You can select an application from the dialog box.
This means that you can write a string by yourself. The system parses the string and calls the application that has registered the corresponding scheme. If there are multiple registered applications, then, a dialog box is displayed for the user to select.
2. Usage
Google officially gave an example: search-samples
The following describes how to use deep-linking in Android.
There are two
...
In the same
@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent intent = getIntent(); String action = intent.getAction(); Uri data = intent.getData();}
GetIntent can be obtained at any time in the Activity's life cycle. However, when other applications call your application, they certainly want to enter an interface of your application or implement a function. Other applications will send this information to you. The best resolution must be onCreate (or onStart, but onStart will be later ). The following suggestions are provided for this official website:
* The deep link should take users directly to the content, without any prompts, interstitial pages, or logins. Make sure that users can see the app content even if they never previously opened the application. It is okay to prompt users on subsequent interactions or when they open the app from the Launcher. This is the same principle as the [first click free](https://support.google.com/webmasters/answer/74536?hl=en) experience for web sites.* Follow the design guidance described in [Navigation with Back and Up](https://developer.android.com/design/patterns/navigation.html) so that your app matches users' expectations for backward navigation after they enter your app through a deep link
Meaning:
1. After opening the application, you should go directly to the content without any prompts, indirect pages, or logon. Make sure that the user can see the content of the application, even if they have never opened the application before. You can prompt the user in subsequent interactions, or open the application in the startup program. This is the same as the principle of free clicking on a website for the first time.
2. Follow the design guidelines for navigation and back-and-up descriptions to align your application with your expectations for entering your application's Deep Navigation through a backward link.
After the above Code is implemented, you can perform the test. During the test, you can use the shell command of adb to perform the test. The syntax format is as follows:
$ adb shell am start -W -a android.intent.action.VIEW -d
For example, the preceding example can be opened as follows:
$ adb shell am start -W -a android.intent.action.VIEW -d "example://gizmos" com.example.android
The above intent can also be set through the web page in the browser. Now the browser will parse this intent and then call the corresponding application, that is, the application can be directly called in the web page.
DeepLink enables interaction between development websites and their own apps. In addition, an intent string can also be sent. For example, if you want to promote your App, you can send this intent to advertisers and then send it to the mobile browser when you click it, use a browser to call your own applications. This best application is still being searched. During the search, when the user finds the corresponding content, the website is usually skipped. However, if DeepLink exists, you can directly jump to your own App through the DeepLink intent, which facilitates both users and developers.
3. DeepLink Principle Analysis 3.1 DeepLinkDispatch framework
DeepLink uses the DeepLinkDispatch framework launched by Airbnb. The DeepLinkDispatch is a framework that implements dispatch jump in the form of annotations. This is a brief introduction to README. md.
3.2 Dispatch framework example
@DeepLink("foo://example.com/deepLink/{id}")public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); if (intent.getBooleanExtra(DeepLink.IS_DEEP_LINK, false)) { Bundle parameters = intent.getExtras(); String idString = parameters.getString("id"); // Do something with idString } }}
Multiple
// Multi-filter annotation @ DeepLink ({"foo: // example.com/deeplink/#id}", "foo: // example.com/anotherDeepLink "}) public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); Intent intent = getIntent (); if (intent. getBooleanExtra (DeepLink. IS_DEEP_LINK, false) {Bundle parameters = intent. getExtras (); String idString = parameters. getString ("id"); // Do something with idString }}}
Annotation of a method:
@DeepLink("foo://example.com/methodDeepLink/{param1}")public static Intent intentForDeepLinkMethod(Context context, Bundle extras) { Uri.Builder uri = Uri.parse(extras.getString(DeepLink.URI)).buildUpon(); return new Intent(context, MainActivity.class) .setData(uri.appendQueryParameter("bar", "baz").build()) .setAction(ACTION_DEEP_LINK_METHOD);}
The above annotation is equivalent to the one registered under the Activity tag in manifest in DeepLink.
Public class DeepLinkReceiver extends BroadcastReceiver {private static final String TAG = "DeepLinkReceiver"; @ Override public void onReceive (Context context, Intent intent) {String deepLinkUri = intent. getStringExtra (DeepLinkHandler. EXTRA_URI); if (intent. getBooleanExtra (DeepLinkHandler. EXTRA_SUCCESSFUL, false) {Log. I (TAG, "Success deep linking:" + deepLinkUri);} else {String errorMessage = intent. getStringExtra (DeepLinkHandler. EXTRA_ERROR_MESSAGE); Log. e (TAG, "Error deep linking:" + deepLinkUri + "with error message +" + errorMessage) ;}} public class YourApplication extends Application {@ Override public void onCreate () {super. onCreate (); IntentFilter intentFilter = new IntentFilter (DeepLinkHandler. ACTION); // use in-app broadcast registration. Do not worry that other applications will receive LocalBroadcastManager. getInstance (this ). registerReceiver (new DeepLinkReceiver (), intentFilter );}}
Next we will analyze its principles.
3.3 source code analysis 3.3.1 generate the corresponding class file according to the Annotation
Click build in AS to generate the corresponding class file. The main files are AS follows:
In the DeepLinkDispatch framework, the DeepLinkDelegate proxy is used to process incoming Uris. In DeepLinkDelegate, The dispatchFrom method is used to process Uris. The Code is as follows:
1. According to getIntent. getData (), the corresponding uri can be obtained.
2. Then, use DeepLinkLoader. load () to load the registered uri. The Code is as follows:
Call loader. parseUri to parse the Uri. After parsing, DeepLinkEntry is returned for our use.
Parse the key-value pair in the Uri, and the code is still in dispatchFrom.
The DeepLinkUri. getParameters code is as follows:
Then the parseParameters of the class is called to obtain the patterns set.
The key is obtained from the intent of DeepLink, and the specific jump content is value.
5. DeepLinkUri. queryParameterNames
QueryParameterNames parses the real Uri into the corresponding annotation, and then implements the distribution logic.
6. Specific Distribution Logic
6.1 first generate the Intent object
6.2 setAction and data: Put action and data into Intent.
6.3 process Bundle.
6.4 call callingActivity.
6.5 startActivity
6.6 createResultAndNotify
The framework of page routing is mostly similar. The main logic is: first register the address page to be routed and the corresponding uri, and then use the uri to initiate the request and then control the central interception to perform matching, when the matching succeeds, the jump is executed. When the matching is successful, the uri can be followed by the data to be transmitted, and then the data interaction can be completed by parsing at the receiving end. After completing the preceding steps, you can call the DeepLink application page and paste the code later.
4. Summary
DeepLink enables direct webpage jump and App jump. In the past, every App on a mobile phone is equivalent to an isolated island, and there is no way to directly jump to a wide range of websites. Now, for example, if you see some wonderful content on an App while browsing Weibo, you can directly click the link to jump to the App (or even judge to enter the App if you follow the App, if it is not installed, enter the App download interface in the App market). This interaction is very convenient, and the App can be connected to the entire online world. In the future, a browser will be able to jump freely. DeepLink can be used in searches. Currently, all searches are for content or webpage calls. In the future, if developers submit their own DeepLink links to the search company, they can directly click the search results to jump to their App when searching for the corresponding results. This can also be applied to advertisements. It is easier to promote your own apps. DeepLink enables large enterprises to interact with each other and redirect each other. If a company has a super App, you can use DeepLink to open a sub-page of your company and hand it over to another App for processing. In this way, other apps are activated. On the basis of DeepLink, Google has a new AppLinks, which means that your website is associated with your App. For example, if you click your website in the text message, you can directly jump to your App without the selection dialog box. Google officially said this:
Android App Links are a special type of deep link that allow your website URLs to immediately open the corresponding content in your Android app (without requiring the user to select the app).To add Android App Links to your app, define intent filters that open your app content using HTTP URLs (as described in [Create Deep Links to App Content]), and verify that you own both your app and the website URLs (as described in this guide). If the system successfully verifies that you own the URLs, the system automatically routes those URL intents to your app.