此方法可以启动一个应用指定的 Activity,不限于默认人口 Activity。但此方法请求的前提多,如下:
知道 App 的包名和 Activity 的全路径及其名称
须要启动的目标 Activity 在 AndroidManifest.xml 中的属性 Export=“true”
- 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
- }
那这种方法下,若何断定目标 Activity 是否存在呢?
下面是网上传播的异常广泛的用法:
遗憾的是, 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 !=
推荐阅读
【编辑推荐】 大年夜法度榜样员到CTO,可能就差这一次改变 知加一周精选:法度榜样员上周薪资播报出炉,快来看看你的热度 终于找到法度榜样员无休止加班的原因了! 揭秘项目经>>>详细阅读
地址:http://www.17bianji.com/lsqh/37221.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示