官术网_书友最值得收藏!

The Android Intent system

Almost every developer knows what an Android Intent system is, but we will explain it a bit and lay out the basic principles required to understand the app links feature. The Android Intent system can be found in the Android platform; this allows the passing of data in a small, simple package. Intent means that we want to perform an action. You may already know about the basic intents:

  • startActivity()
  • startActivityForResult()
  • startService()
  • sendBroadcast()

The following figure shows an Android Intent system for the startActivity() and onCreate() intents:

Source: http://developer.android.com/guide/components/intents-filters.html

The Android Intent system is a flexible mechanism that is used to enable apps to handle content and requests. Multiple apps may declare matching URI (short for Uniform Resource Identifier) patterns in their intent filters. When a user clicks on a web link that does not have a default launch handler, the platform may display a dialog for the user to select from a list of apps that have declared matching intent filters.

Intents are also used to trigger actions across the system, and some of these actions are system-defined, such as ACTION_SEND (referred to as the share action), where you as an app developer can share/send specific information to another application in order to complete an action required by a user.

Until Android Marshmallow, browsers handled each link clicked on the Web, and the system checked whether a custom URI scheme was available. Your application could handle specific custom actions via the custom URI scheme. This was tricky at times and didn't allow the handling of links under an entire web domain. Now, it's possible. Android Marshmallow's added support for app links allows you, as an app developer, to associate an app with a web domain. Automatically, this will allow you to determine the default app that will handle a particular web link instead of showing the selected application to handle the dialog.

Note

If you wish to read more about intents, you can go to the following link:

http://developer.android.com/guide/components/intents-filters.html

Creating a website association

You as an app developer as well as a website owner need to declare a website association with an app. The declaration is done by hosting a JSON file, which is specifically named assetlinks.json. The file must reside in a specific location on the domain in question, such as:

https://<domain>:<optional port>/.well-known/assetlinks.json

Note

This file is accessed and verified over the HTTPS protocol and not HTTP.

Why this file?

The JSON file holds information about the Android application that will be the default handler for the URLs under this domain. In the JSON file, you must have the following structure:

[{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "com.yourapp.androidapp",
    "sha256_cert_fingerprints": [""]
  }
}]

The following are some elements of the preceding structure:

  • package_name: This is the package name from your app's manifest
  • sha256_cert_fingerprints: This is the SHA-256 fingerprint of your app

    Use the following command if you don't have this SHA (short for Secure Hash Algorithm):

    keytool -list -v -keystore app_release_signing.keystore
    

Triggering app link verification

You can request automatic verification for any app links declared in the assetlinks.json file. Requesting a verification is done by adding the android:autoVerify attribute to each intent filter in the manifest and setting it to true.

Let's say we own a WhatsApp application and domain. We want to autoverify an intent filter that has the android.intent.action.VIEW action.

The following is a sample activity from WhatsApp that handles app links and the autoverification attribute:

<activity android:name="com.whatsapp.XXX" …>
  <intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="http" android:host="www.whatsapp.com"/>
    <data android:scheme="https" android:host="www.whatsapp.com"/>
  </intent-filter>
</activity>

The android:autoVerify attribute alerts the platform to verify app links when the app is installed. If the app link's verification fails, your app will not be set as the preferred app to handle these links. If there is no preferred app to handle these links whenever a user opens one of them, a dialog to choose an app is displayed.

If a user has used the system settings and set an app as the preferred app, then the link will go directly to the app but not because the verification was successful.

App link settings and management

For easy management, you can enter the system settings and tweak the URL handling by navigating to Settings | Apps | App info | Open by default.

主站蜘蛛池模板: 德化县| 宣化县| 仁怀市| 天柱县| 孝感市| 石林| 湘潭县| 株洲县| 黔东| 莒南县| 剑河县| 甘谷县| 平阴县| 淮滨县| 安龙县| 府谷县| 涿州市| 肃宁县| 吴江市| 唐海县| 库伦旗| 托克逊县| 南召县| 昌图县| 米泉市| 新邵县| 寿阳县| 陵水| 永丰县| 铜山县| 军事| 上林县| 河津市| 壤塘县| 武宁县| 靖边县| 沧州市| 尖扎县| 深圳市| 微博| 济源市|