安卓通过url打开app,以及下载对应app

本文介绍了如何在Android中使用自定义URI Scheme打开应用以及通过包名启动第三方应用的方法。包括URI的组成部分、注册自定义URI的过程及解析传入值的代码示例。同时,还提供了当目标应用未安装时引导用户下载的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.使用Custom Scheme URI打开APP

此红色部分(自定义Uri)引用的博客是:http://blog.youkuaiyun.com/buptlzx/article/details/9837137

    就Android平台而言,URI主要分三个部分:scheme, authority and path。其中authority又分为host和port。格式如下:

scheme://host:port/path
举个实际的例子:
content://com.example.project:200/folder/subfolder/etc ?arg0=1&arg1=2
———/ —————————/ —/ ————————–/ ————————–/(query我们要传的值)
scheme host port path (我们要传的值如果是传多个值使用&)
——————————–/
authority

  1. android代码而言:主要有两块一部分是注册文件如下。
            <intent-filter>
                <data
                    android:host="com.example.project"
                    android:port="200"
                    android:scheme="content" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>

2.另一部分就是app首页解析网页代码如下。

 Intent intent = getIntent();  
        Uri uri=intent.getData();  
        if(uri!=null){  
            String name=uri.getQueryParameter("arg0");  
            String name1=uri.getQueryParameter("arg1");  
            String scheme= uri.getScheme();  
            String host=uri.getHost();  
            String port=uri.getPort()+"";  
            String path=uri.getPath();  
            String query=uri.getQuery();  
        }

二.通过包名打开第三方app,如果没有包名就去指定地址下载对应的app

    try {
            Intent intent = wm.getPackageManager().getLaunchIntentForPackage(appPackageName);
            Bundle bundle = new Bundle();
            bundle.putString("s","d");
            intent.putExtras(bundle);
            wm.startActivity(intent);
            return;
        } catch (Exception e) {
            downloadAddress = downloadAdd;
            BaseDialog dialog = new BaseDialog();
            dialog.setTitle("提示");
            dialog.setContent("您还未下载“" + DelegateDataBase.ENTRUST_LIST[index] + "”是否进行下载?");
            dialog.setConfirm("下载", new BaseDialog.DialogListener() {
                @Override
                public void onListener() {
                        Uri uri = Uri.parse(downloadAddress.toString());
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        wm.startActivity(intent);
                    }

            });
            dialog.setCancel("取消", null);
            dialog.show(wm);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水的川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值