Android 从浏览器链接打开App

2020-04-13  本文已影响0人  雨落川川

需求很简单,通过Html页面中的一个超链接打开我们的App,实现也很简单。
网页:

<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a> 

scheme:代表Scheme的协议名称(必要)

host和path可选择添加

query:代表URL传递的数据
简单的写一个页面:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>StartApp</title>
    <a href="newfp://jp.app/startAs?name=text&pwd=26">打开我们的应该用</a>
</head>
<body>
</body>
</html>

接卸来开始配置AndroidManifest.xml文件,在有<action android:name="android.intent.action.MAIN" />的actvity配置下新增一个filter,注意是新增一个filter,例如:

      <activity android:name=".activity.LoginActivity">
            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="newfp" android:host="jp.app" android:pathPrefix="/startAs"/>
            </intent-filter>
        </activity>

这里注意scheme里参数和我们在html页面里的对应关系,不要弄错,多了,我们在a标签里还传递了参数,接受也很简单,例如:

  Intent intent=getIntent();
        if (intent!=null){
            String myAction=intent.getAction();
            if(Intent.ACTION_VIEW.equals(myAction)){
                Uri uri = intent.getData();
                if(uri != null){
                    String name = uri.getQueryParameter("name");
                    String pwd= uri.getQueryParameter("pwd");
                    System.out.println("测试信息:"+name+":"+pwd);
                }
            }
        }

写完,我们来看看效果:



成功实现,控制台的日志也能看到我们的参数确实传递过来了:

2020-04-13 10:52:53.033 21748-21748/com.xxx I/System.out: 测试信息:text:26

就这样,记录下来,收工。

上一篇 下一篇

猜你喜欢

热点阅读