zoukankan      html  css  js  c++  java
  • android程序中实现打开另一个app

    1、已知要打开的apk的包名

    String packetName = "com.onedollar.smartnurse";
    Intent intent = getActivity().getPackageManager()
            .getLaunchIntentForPackage(packetName);
    // 这里如果intent为空,就说名没有安装要跳转的应用
    if (intent != null) {
        if (!getActivity().getPackageName().equals(packetName)) {
            startActivity(intent);
        } else {
            ToastView.showToast("你已经在当前应用中");
        }
    } else {//打开应用市场
        Uri uri = Uri
                .parse("market://details?id=" + packetName);
        intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            getActivity().startActivity(intent);
        } catch (Exception e) {
            ToastView.showToast("打开应用市场失败");
        }
    }

     2.配置scheme和host

    在AndroidManifest.xml文件中,为要启动的页面配置过滤器,添加如下代码:

    <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:host="DaMeiShangRao"
          android:scheme="DangZheng" />  
    </intent-filter>

    如果是启动Activity,不能把原来的过滤器覆盖掉,及在原因过滤下添加上述代码

               <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:host="test"
                        android:scheme="myapp" />
                </intent-filter>

    在activtity中通过uri调用该页面,uri的组成为[scheme]://[host]

    Uri uri=Uri.parse("myapp://test");
            Intent intent= new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);

    配置scheme和host后,可以在浏览器通过点击链接打开客户端 <a href="myapp://test" >打开客户端</a>

     注意:scheme和host必须小写

  • 相关阅读:
    RobotFramework执行报“FOR loop contains no keywords.”--踩坑记录1
    RIDE控制台中文内容显示乱码问题解决方式记录
    Sublime Text 3激活注册码 (亲测可用)
    Python项目1:实现将图片转化为手绘效果
    Cookie和Session学习笔记
    Robot Framework离线安装(附图)
    Windows使用Nginx搭建RTMP服务器
    Android:系统设置出厂默认值配置
    Android:检测内存泄漏的自动化测试Python脚本
    Shell脚本实现延迟执行指令
  • 原文地址:https://www.cnblogs.com/3A87/p/4867604.html
Copyright © 2011-2022 走看看