使用Intent打开第三方应用及验证可用性

移动开发 Android
本文主要讲述,使用Intent打开第三方应用或指定Activity的三种方式、使用上面三种方式时分别如何判断该 Intent 能否被解析以及判断该 Intent 能否被解析中可能出现的遗漏。

本文主要记录:

  • 使用 Intent 打开第三方应用或指定 Activity 的三种方式
  • 使用上面三种方式时分别如何判断该 Intent 能否被解析
  • 判断该 Intent 能否被解析中可能出现的遗漏

基础知识

1. App 的入口 Activity 与其 icon

[[202713]] 

一个普通的应用默认会有一个入口 Activity,它在 AndroidManifest.xml 中一般这样写: 

<application>   
    <activity android:name=".MainActivity" >   
        <intent-filter>   
            <action android:name="android.intent.action.MAIN" />   
            <category android:name="android.intent.category.LAUNCHER" />   
        </intent-filter>   
    </activity>   
    ...   
</application>  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

只有配置了一个这样的 Activity,这个应用才会点击的时候知道启动哪个 Activity,如果把 category 的值修改为 android.intent.category.DEFAULT 那么,这个应用将在桌面看不到 icon,无法直接打开了。

使用 Intent 打开第三方应用或指定 Activity 的方式

只知道包名 - 需要有默认的入口 Activity

启动指定第三方应用的 Activity - 需要包名和 Activity 名,且该 Activity 的 Export=“true”

隐式启动第三方应用

1. 使用 PackageManager.getLaunchIntentForPackage() 

String package_name="xx.xx.xx";   
PackageManager packageManager = context.getPackageManager();   
Intent it = packageManager.getLaunchIntentForPackage(package_name);   
startActivity(it);  
  • 1.
  • 2.
  • 3.
  • 4.

该方法针对只知道包名,想要启动该应用时使用,对该应用的***限制是 有默认的入口 Activity 。

当没有默认的入口 Activity 时,会报 NullPointerException 异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.toString()' on a null object reference  
  • 1.

再看看 getLaunchIntentForPackage() 方法的说明: 

/**   
     * Returns a "good" intent to launch a front-door activity in a package.   
     * This is used, for example, to implement an "open" button when browsing   
     * through packages.  The current implementation looks first for a main   
     * activity in the category {@link Intent#CATEGORY_INFO}, and next for a   
     * main activity in the category {@link Intent#CATEGORY_LAUNCHER}. Returns  
     * <code>null</code> if neither are found.  
     *   
     * @param packageName The name of the package to inspect. 
     *   
     * @return A fully-qualified {@link Intent} that can be used to launch the   
     * main activity in the package. Returns <code>null</code> if the package   
     * does not contain such an activity, or if <em>packageName</em> is not   
     * recognized.   
     */  
 
    public abstract Intent getLaunchIntentForPackage(String packageName);   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

所以使用此方式判定 Intent 是否为空即可。

String package_name = "xx.xx.xx";   
PackageManager packageManager = getPackageManager();   
Intent it = packageManager.getLaunchIntentForPackage(package_name);   
if (it != null){   
    startActivity(it);   
}else{   
    //没有默认的入口 Activity   
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

2. 使用 Intent.setComponent()

String package_name = "xx.xx.xx";   
String activity_path = "xx.xx.xx.ab.xxActivity";   
Intent intent = new Intent();   
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//可选   
ComponentName comp = new ComponentName(package_name,activity_path); 
intent.setComponent(comp);   
startActivity(intent);  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

此方式可以启动一个应用指定的 Activity,不限于默认入口 Activity。但此方式要求的条件多,如下:

知道 App 的包名和 Activity 的全路径及其名称

需要启动的目标 Activity 在 AndroidManifest.xml 中的属性 Export=“true”

那这种方式下,如何判断目标 Activity 是否存在呢?

下面是网上流传的非常普遍的用法: 

String package_name = "xx.xx.xx";   
String activity_path = "xx.xx.xx.ab.xxActivity";   
Intent intent = new Intent();   
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//可选   
ComponentName cn = new ComponentName(package_name,activity_path);   
intent.setComponent(cn);    
 
if (intent.resolveActivity(getPackageManager()) != null) {   
    startActivity(intent);   
else {   
    //找不到指定的 Activity   

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

遗憾的是, Intent.resolveActivity() 方法并不能判定此方式所要启动的 Activity 是否存在,如果此 Activity 不存在,会报 java.lang.IllegalArgumentException: Unknown component 异常,并导致程序崩溃。

下面看下 resolveActivity() 的代码,以及它的 相似方法 resolveActivityInfo() : 

public ComponentName resolveActivity(PackageManager pm) {  
    if (mComponent != null) {  
        return mComponent;  
    }  
    ResolveInfo info = pm.resolveActivity(this,  
        PackageManager.MATCH_DEFAULT_ONLY);  
    if (info != null) {  
        return new ComponentName(  
            info.activityInfo.applicationInfo.packageName,  
            info.activityInfo.name);  
    }  
    return null;  
}    
 
public ActivityInfo resolveActivityInfo(PackageManager pm, int flags) {   
    ActivityInfo ai = null;   
    if (mComponent != null) {   
        try {   
            ai = pm.getActivityInfo(mComponent, flags);   
        } catch (PackageManager.NameNotFoundException e) {   
            // ignore   
        }  
 
    } else {   
        ResolveInfo info = pm.resolveActivity(this,   
            PackageManager.MATCH_DEFAULT_ONLY | flags);   
        if (info != null) {   
            ai = info.activityInfo;   
        }   
    }    
 
    return ai;  
 
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.

显而易见,我们此方式就是先设置的 ComponentName,所以会直接 return mComponent 给我们,并没有任何判定的逻辑。相对的, resolveActivityInfo() 则可以进行有效判定并返回 null。故,我们选择使用 Intent.resolveActivityInfo() 进行此方式下的判定:

String package_name = "xx.xx.xx";  
 
String activity_path = "xx.xx.xx.ab.xxActivity";  
 
Intent intent = new Intent();  
 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//可选  
 
ComponentName cn = new ComponentName(package_name,activity_path);  
 
intent.setComponent(cn);    
 
if (intent.resolveActivityInfo(getPackageManager(), PackageManager.MATCH_DEFAULT_ONLY) != null) {  
 
    startActivity(intent);  
 
else {  
 
    //找不到指定的 Activity  
 
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

3.隐式启动第三方应用

此方式多用于启动系统中的功能性应用,比如打电话、发邮件、预览图片、使用默认浏览器打开一个网页等。 

> Intent intent = new Intent();  
 
> intent.setAction(action);  
 
> intent.addCategory(category);  
 
> intent.setDataAndType("abc://www.dfg.com","image/gif");  
 
> startActivity(intent);  
 
>  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

条件1:IntentFilter 至少有一个 action 至少有一个Category,可以没有 Data 和 Type

条件2:如果有 Data,参数中 Data 必须符合 Data 规则

条件3:Action 和 Category 必须同时匹配 Activity 中的一个 Action 和一个 Category (Category 默认:android.intent.category.DEFAULT)

隐式启动功能繁多,就不一一列举了,需要时直接搜索相关代码即可,我们用打开一个网页为例:

Uri uri = Uri.parse("http://www.abc.xyz");  
 
Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
 
startActivity(intent);  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

这时,直接使用 Intent.resolveActivity() 方法没什么问题: 

Uri uri = Uri.parse("http://www.abc.xyz");  
 
Intent intent = new Intent(Intent.ACTION_VIEW, uri);   
 
if (intent.resolveActivity(getPackageManager()) != null) {   
    startActivity(intent);   
else {   
    // 没有安装所需应用   
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

总结

经过阅读 PackageManager 的代码,发现还可以使用 packageManager.queryIntentActivities() 方法判断系统里是否有能解析指定 Intent 的应用。

public boolean isAvailable(Context context, Intent intent) {  
 
    PackageManager packageManager = context.getPackageManager();  
 
    List list = packageManager.queryIntentActivities(intent,  
 
    PackageManager.MATCH_DEFAULT_ONLY);  
 
    return list.size() > 0;  
 
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

那么,总结下来就是:

方式一 PackageManager.getLaunchIntentForPackage() ,直接判断返回的 Intent 是否为空即可;

方式二 Intent.setComponent() ,使用 Intent.resolveActivityInfo() 或者 packageManager.queryIntentActivities() 两种方式;

方式三 隐式启动,使用 Intent.resolveActivity() 、 Intent.resolveActivityInfo() 、 packageManager.queryIntentActivities() 三种方式均可。 

责任编辑:庞桂玉 来源: Android技术之家
相关推荐

2017-09-04 20:24:35

Activity代码三种方式

2016-12-28 17:47:56

API

2015-11-05 16:44:37

第三方登陆android源码

2013-08-12 16:04:19

第三方移动应用

2013-03-26 09:50:51

2014-07-22 10:56:45

Android Stu第三方类库

2010-05-25 11:09:31

SVN工具

2019-09-02 14:59:41

苹果维修设备

2012-08-03 09:44:11

iOS 6苹果地图

2021-03-12 16:35:33

鸿蒙HarmonyOS应用

2010-08-02 23:20:24

2014-07-23 08:55:42

iOSFMDB

2019-07-30 11:35:54

AndroidRetrofit

2012-05-24 21:59:55

iOS

2011-06-07 14:36:24

iOS5WWDC

2010-08-31 17:12:13

2024-06-14 11:59:12

2019-09-03 18:31:19

第三方支付电商支付行业

2013-12-16 17:20:21

Chrome登录验证

2014-08-13 10:27:23

CocoaPods
点赞
收藏

51CTO技术栈公众号