android 安装 APK
来源:互联网 发布:神经网络算法matlab 编辑:程序博客网 时间:2024/06/03 18:08
安装APK:
public static void installApp(Context context, String packageName) { try { PackageInfo pi = context.getPackageManager().getPackageInfo(packageName, 0); Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null); resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER); resolveIntent.setPackage(pi.packageName); List<ResolveInfo> apps = context.getPackageManager().queryIntentActivities(resolveIntent, 0); if (!apps.isEmpty()) { ResolveInfo ri = apps.get(0); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name)); context.startActivity(intent); } } catch (NameNotFoundException e) { // no-op } }
一些用到的细节:
Intent的setPackage 可以限制 Intent只能投递到某个Package内的Component.
/** * (Usually optional) Set an explicit application package name that limits * the components this Intent will resolve to. If left to the default * value of null, all components in all applications will considered. * If non-null, the Intent can only match the components in the given * application package. * * @param packageName The name of the application package to handle the * intent, or null to allow any application package. * * @return Returns the same Intent object, for chaining multiple calls * into a single statement. * * @see #getPackage * @see #resolveActivity */ public Intent setPackage(String packageName) {}
Intent的setComponent则限制Intent只能投递给某个Component, 和setClass干的事情一样,setClass的注释也说了:
* Convenience for calling {@link #setComponent(ComponentName)} with the
* name returned by a {@link Class} object.
ResolveInfo内部封装了对Intent解析<通过queryIntentActivities>出来的相关的Activity的info:ActivityInfo
0 0
- Android 下载APK 安装APK 打开APK
- Android 下载APK 安装APK 打开APK
- Android 下载APK 安装APK 打开APK
- Android 下载APK 安装APK 打开APK
- android安装卸载apk
- Android apk安装过程
- 关于android apk安装
- Android模拟器安装APK
- android 安装apk代码
- android apk 覆盖安装
- Android 下载安装APK
- Android 如何安装apk
- Android apk安装方式
- android批量安装APK
- Android安装/卸载apk
- Android APK安装过程
- android 安装apk文件
- android模拟器安装apk
- 我的2015
- 我对Tomcat数据源的理解
- C++11中的Tuple和可变参数模版
- 《Little Runner》(六)——创建分数标签、血量条
- java反射(一):反射基础
- android 安装 APK
- Python中的字符串执行函数
- Grava 工具
- 这是一个测试文章
- 强大的分组SectionListView, 支持自定义组头布局和分组的内容布局, 组头有挤压效果
- 《第一行代码-Android》学习笔记(十八)
- JDOM,DOM4J,SAX,DOM,JAXP比较
- Java中的日期操作
- SVN