网页打开APP

2018-04-27  本文已影响12人  zhengLH

【背景需求】在一个网页,点击链接,打开app中某一个页面,如果手机存在该app,则直接打开,若不存在,则打开网页。

【1】html 文件
【详解】 aaa://lee.com:8080/mypath?name=lee&age=24
其中,aaa:协议头(scheme), lee.com:域名(host)
8080:端口号(port), mypath: 目录路径(path)
?name=lee&age=24 参数名(以 键值对)

<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title> URL打开APP </title>
</head>

<body>
<a href="aaa://lee.com:8080/mypath?name=lee&age=24"> 网页打开App    </a>
</body>
</html>

【2】AndroidManifest.xml 配置

 <intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.APP_BROWSER"/>
    <data android:scheme="aaa" android:host="/aaa.lee.com"/>
  </intent-filter>

【3】获取 传值

/**
* @Author Lee
* @Time 2018/4/27
* @Theme  网页打开APP
*/

public class URLToLauchAppActivity extends AppCompatActivity {

private TextView mTvContent;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activit_url_lauch_app);
    initView();
}

private void initView() {

    mTvContent = findViewById(R.id.tv_content);

    Intent intent = getIntent();
    Uri uri = intent.getData();
    if(uri != null){

        String name = uri.getQueryParameter("name");
        String age = uri.getQueryParameter("age");

        String scheme = uri.getScheme();
        String host = uri.getHost();
        String port = uri.getPort() + "";
        String path = uri.getPath();
        String query = uri.getQuery();

        mTvContent.setText( "name= " + name + "/n"
                + "age= " + age  + "/n"
                + "scheme= "+ scheme + "/n"
                + "host= " + host + "/n"
                + "port= " + port + "/n"
                + "path= " + path + "/n"
                + "query= " + query);

    }
   }
}

【來源】
(1)yq.aliyun.com/articles/57088

(2)www.sohu.com/a/16889210_150943

上一篇下一篇

猜你喜欢

热点阅读