android setcomponent 作用,Android 必知必会

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

本文主要记录:使用 Intent 打开第三方应用或指定 Activity 的三种方式

使用上面三种方式时分别如何判断该 Intent 能否被解析

判断该 Intent 能否被解析中可能出现的遗漏

基础知识

1. App 的入口 Activity 与其 icon

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

2

3

4

5

6

7

8

9

10

...

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

使用 Intent 打开第三方应用或指定 Activity 的方式只知道包名 - 需要有默认的入口 Activity

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

隐式启动第三方应用

1. 使用 PackageManager.getLaunchIntentForPackage()1

2

3

4String package_name="xx.xx.xx";

PackageManager packageManager = context.getPackageManager();

Intent it = packageManager.getLaunchIntentForPackage(package_name);

startActivity(it);

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

当没有默认的入口 Activity 时,会报 NullPointerException 异常:1java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.toString()' on a null object reference

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16* 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

* null 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 null if the package

* does not contain such an activity, or if packageName is not

* recognized.

*/

public abstract Intent (String packageName);

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

2

3

4

5

6

7

8String package_name = "xx.xx.xx";

PackageManager packageManager = getPackageManager();

Intent it = packageManager.getLaunchIntentForPackage(package_name);

if (it != null){

startActivity(it);

}else{

//没有默认的入口 Activity

}

2. 使用 Intent.setComponent()1

2

3

4

5

6

7String 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);

此方式可以启动一个应用指定的 Activity,不限于默认入口 Activity。但此方式要求的条件多,如下:知道 App 的包名和 Activity 的全路径及其名称

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

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

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

2

3

4

5

6

7

8

9

10

11

12String 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

}

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

下面看下 resolveActivity() 的代码,以及它的 相似方法 resolveActivityInfo() :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

34public 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;

}

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

2

3

4

5

6

7

8

9

10

11

12String 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

}

3.隐式启动第三方应用

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

2

3

4

5

6> Intent intent = new Intent();

> intent.setAction(action);

> intent.addCategory(category);

> intent.setDataAndType("abc://www.dfg.com","image/gif");

> startActivity(intent);

>条件1:IntentFilter 至少有一个 action 至少有一个Category,可以没有 Data 和 Type条件2:如果有 Data,参数中 Data 必须符合 Data 规则条件3:Action 和 Category 必须同时匹配 Activity 中的一个 Action 和一个 Category (Category 默认:android.intent.category.DEFAULT)

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

2

3Uri uri = Uri.parse("http://www.abc.xyz");

Intent intent = new Intent(Intent.ACTION_VIEW, uri);

startActivity(intent);

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

2

3

4

5

6

7

8Uri uri = Uri.parse("http://www.abc.xyz");

Intent intent = new Intent(Intent.ACTION_VIEW, uri);

if (intent.resolveActivity(getPackageManager()) != null) {

startActivity(intent);

} else {

// 没有安装所需应用

}

总结

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

2

3

4

5

6public boolean isAvailable(Context context, Intent intent){

PackageManager packageManager = context.getPackageManager();

List list = packageManager.queryIntentActivities(intent,

PackageManager.MATCH_DEFAULT_ONLY);

return list.size() > 0;

}

那么,总结下来就是:方式一 PackageManager.getLaunchIntentForPackage(),直接判断返回的 Intent 是否为空即可;

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

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

如果有什么建议或者问题可以随时联系我,共同探讨学习:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值