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

原文地址:https://www.cnblogs.com/3A87/p/4867604.html