编程知识 cdmana.com

Android configures scheme to use browser to call up app and solve the problem that does not work

This article has been included in Android A collection of miscellaneous diseases ( Continuous updating )

1、Scheme The role of , Browsers evoke APP

It is common to open a website in a mobile browser , There will be a App Inside the open button , Click directly to open APP And jump to the details page , It's pretty much the same Scheme.Scheme The basic use of :

<activity android:name=".MainActivity">
    <!--  You need to add the following intent-filter To configure  -->
    <intent-filter>
        ...
        <data
            android:scheme="myscheme"
            android:host="myhost"
            android:port="1024"
            android:path="/main" />
    </intent-filter>
</activity>

stay web Add a click event jump to the page :

<a href="myscheme://myhost:1024/main?key1=value1&key2=value2">open app with Uri Scheme</a>

Aroused MainActivity in , Can be in onCreate(APP Not opened ) and onNewIntent(APP Already opened ) Method to get the jump link :

Intent intent = getIntent();
if (null != intent && null != intent.getData()) {
    Uri uri = intent.getData();
    Log.e(TAG, "uri=" +uri);
    String scheme = uri.getScheme();
    String host = uri.getHost();
    int port = uri.getPort();
    String path = uri.getPath();
    String key1 = uri.getQueryParameter("key1");
    String key2 = uri.getQueryParameter("key2");
    Log.e(TAG, "scheme=" + scheme + ",host=" + host
            + ",port=" + port + ",path=" + path
            + ",query=" + uri.getQuery()
            + ",key1=" + key1 + ",key2=" + key2);
}

Print the message as follows :

uri=myscheme://myhost:1024/main?key1=value1&key2=value2
scheme=myscheme,host=myhost,port=1024,path=/main,query=key1=value1&key2=value2,key1=value1,key2=value2

2、 The causes and solutions of ineffective problems encountered

Add... To the application Scheme, Excitedly start the browser and press the nuclear launch button , As a result, the atmosphere gradually became awkward ...

What's the problem ? Carefully compared the link address 、AndroidManifest Configuration of , Heaven! , It's clearly the same , It's devastating !!!

And then a chance , It was used AS Self contained Android In the simulator web test APP Open this link , Open a new page , Shows the set link address , The cause of the problem has been found !!!

I am here web The link set in the page is MyScheme://MyHost, Click on the page to show the link is myshceme://myhost, Something magical , A search found an article , This elder is in the same situation as I am , The article links below :

H5 Can't adjust android app The pit of scheme Case write

The end result is ,Scheme Never use capital letters for links , Include scheme、host、port、path.

版权声明
本文为[coder-ice]所创,转载请带上原文链接,感谢

Scroll to Top