force open android app through schema

2019-07-27 13:15发布

Here is my story. I have an android app that shares my website link. on Android devices I want to check if the user has my android app open android application and show corresponding content to that link, but if do not have my app open up that link in the browser. I use DeepLinkDispatch for this, but I have a problem, when user click on shared link, android ask client to choose which app open this link (include my app), but i want to force open my app. (if installed on device)

2条回答
Evening l夕情丶
2楼-- · 2019-07-27 13:59

You have to define the intent category, default and browsable with android:scheme, android:host and android:pathPrefix in the activity intent filter in the manifest file with action view. For example if the url is : “http://www.example.com/gizmos”. Based on the host, scheme and path prefix in the data android will find the intent that can handle the url. To enable the os to find your intent you have to add following filter in the app.

<activity
android:name="com.example.android.GizmosActivity"
android:label="@string/title_gizmos" >
<intent-filter android:label="@string/filter_title_viewgizmos">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
    <data android:scheme="http"
          android:host="www.example.com"
          android:pathPrefix="/gizmos" />
    <!-- note that the leading "/" is required for pathPrefix-->
    <!-- Accepts URIs that begin with "example://gizmos” -->
    <data android:scheme="example"
          android:host="gizmos" />
</intent-filter>

Test the deep link: You can test the deeplink from shell as follows:

adb shell am start -W -a android.intent.action.VIEW -d "http://www.example.com/gizmos" packageIDofYourApp

You can dig deeper on deplinking at developer site

Verify if your App can receive the intent You can check if your app can handle the intent having the specific url using package manager:

Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("http://www.example.com/gizmos"));

    PackageManager packageManager = getPackageManager();
    List<ResolveInfo> activities = packageManager.queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    boolean isIntentSafe = activities.size() > 0;
    System.out.println("print pack:"+ ((isIntentSafe == true) ? activities.get(0).activityInfo.packageName : "No package found"));
查看更多
等我变得足够好
3楼-- · 2019-07-27 14:00

That is not possible prior to Android 6.0.

On Android 6.0+, you can can use automatic verification of app links to request that your app be the one to handle certain URLs, when they are used in ACTION_VIEW Intents.

However:

  • A Web browser does not have to use ACTION_VIEW for a URL. A Web browser is welcome to just load the Web page identified by the URL.

  • I do not know if app link verification works for Intent actions other than ACTION_VIEW. Your question suggests that the URL might be used in ACTION_SEND instead.

  • Users can go into Settings and prevent your app from handling URLs.

查看更多
登录 后发表回答