Android学习笔记---通过网页唤醒自己的APP并且传参

前言

相信大家在生活中会发现很多网页都有这样的功能,前一秒还在网页呢 后一秒莫名其妙的就进入了他们的APP,那么这种效果到底是如何做的呢?
经过本屌丝认真学习后发现是使用安卓中android:scheme属性来完成功能的,这里说一下我个人看法,我觉得android:scheme属性很想Intent的隐式匹配,或者说他就是隐式的一种,言归正传 接下来我们来看一下到底咋用

实践部分

首先我们先新建一个项目并且在AndroidManifest.xml中加入android:scheme属性

            <!--增加如下属性,其中crack是唤醒程序的action-->
            <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="crack"/>
            </intent-filter>

现在我们在MainActivity中获取网页转过来的参数

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text_ht);
        Intent intent = new Intent();
        //获取参数详情
        Uri uri = intent.getData();
        if(uri != null){
            textView.setText(uri.getPath());
            String host = uri.getHost();
            String dataString = intent.getDataString();
            String id = uri.getQueryParameter("d");
            String path = uri.getPath();
            String path1 = uri.getEncodedPath();
            String queryString = uri.getQuery();
            System.out.println("host:"+host);
            System.out.println("dataString:"+dataString);
            System.out.println("id:"+id);
            System.out.println("path:"+path);
            System.out.println("path1:"+path1);
            System.out.println("queryString:"+queryString);
        }
    }
}

现在咱们APP该做的视频已经做完了,现在来搞网页的
新建一个ht.html

<html>
    <head>
        <meta http-equiv=content-type content="text/html;charset=utf-8">
         <title>网页唤醒APP</title>
    </head>
    <body>
        <input id="txt"/>
        <input type="button" value="提交" onclick="sub()">
    </body>
    <script>
            //获取内容
            function sub(){
                var content = document.getElementById("txt");
                //此处注意其中crack替换为你APP中设置的名字
                var str = 'crack://crack.gm:80/'+content.value+'?p=91&d=15';
                openUrl(str);
            }
            //唤醒APP
             function openUrl(url){
            window.open(url);
        }
    </script>
</html>

接下来 我们来运行一下看看效果

这里写图片描述

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值