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必须小写

  • 相关阅读:
    Ubuntu 出现 apt-get问题的解决方法
    Conversion to Dalvik format failed: Unable to execute dex: null
    java中判断是否为中文
    解决:Unable to connect to repository https://dl-ssl.google.com/android/eclipse/site.xml
    Android 获取本机WIFI及3G网络IP
    xp重装系统后恢复Linux启动
    Android中空格及换行
    ubuntu12.10设置thunderbird开机自启动
    web安全及防护
    回归基础: JavaScript 变量提升
  • 原文地址:https://www.cnblogs.com/3A87/p/4867604.html
Copyright © 2011-2022 走看看