Android 安装应用的两种方式--外部应用安装器安装和静默安装(系统应用)
2017-06-09 15:41
441 查看
1.调用外部安装器安装
2.静默安装(系统应用才有权限)
这里使用反射的方式调用
3.监听应用的安装与卸载
//注册监听
//移除监听
//广播接收回调
/** * 外部应用安装器安装apk(原生接口) * @param context * @param path apk的路径 * @return */ public static boolean installApkByPath(Context context, String path) { try { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); return true; } catch (Exception e) { e.printStackTrace(); } return false; }
2.静默安装(系统应用才有权限)
这里使用反射的方式调用
public class PackageManager_R { private static final String TAG = "PackageManager_R"; private static final String ERROR_TAG = "ReflectError " + TAG; private static Method sMethod_installPackage; /** * 静默安装(原生接口) * * @param path * @return */ public static boolean installPackage(String path, final SystemAppInstall.InstalledCallBack callBack) { if (sMethod_installPackage == null) { try { sMethod_installPackage = PackageManager.class.getMethod("installPackage", Uri.class, IPackageInstallObserver.class, int.class, String.class); } catch (Exception e) { LogUtils.w(ERROR_TAG, "", e); e.printStackTrace(); } } IPackageInstallObserver observer = new IPackageInstallObserver.Stub() { public void packageInstalled(String packageName, int returnCode) { if (returnCode != 1) { callBack.onResult(packageName, false); } else { callBack.onResult(packageName, true); } } }; PackageManager pm = AppContextUtils.getAppContext().getPackageManager(); try { sMethod_installPackage.invoke(pm, Uri.parse("file://" + path), observer, 0, null); return true; } catch (Exception e) { LogUtils.w(ERROR_TAG, "", e); e.printStackTrace(); } return false; } }对应的源码的接口
/** * @hide Install a package. Since this may take a little while, the result * will be posted back to the given observer. An installation will * fail if the calling context lacks the * {@link android.Manifest.permission#INSTALL_PACKAGES} permission, if * the package named in the package file's manifest is already * installed, or if there's no space available on the device. * @param packageURI The location of the package file to install. This can * be a 'file:' or a 'content:' URI. * @param observer An observer callback to get notified when the package * installation is complete. * {@link IPackageInstallObserver#packageInstalled(String, int)} * will be called when that happens. This parameter must not be * null. * @param flags - possible values: {@link #INSTALL_FORWARD_LOCK}, * {@link #INSTALL_REPLACE_EXISTING}, * {@link #INSTALL_ALLOW_TEST}. * @param installerPackageName Optional package name of the application that * is performing the installation. This identifies which market * the package came from. * @deprecated Use {@link #installPackage(Uri, PackageInstallObserver, int, * String)} instead. This method will continue to be supported * but the older observer interface will not get additional * failure details. */ // @SystemApi public abstract void installPackage( Uri packageURI, IPackageInstallObserver observer, int flags, String installerPackageName);
3.监听应用的安装与卸载
//注册监听
IntentFilter installFilter = new IntentFilter(); installFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); installFilter.addAction(Intent.ACTION_PACKAGE_ADDED); installFilter.addDataScheme("package"); mContext.registerReceiver(mAppReceiver, installFilter);
//移除监听
mContext.unregisterReceiver(mAppReceiver);
//广播接收回调
private BroadcastReceiver mAppReceiver = new BroadcastReceiver() { 998c @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); LogUtils.d(TAG, "onReceive = " + action); String pkgName = intent.getData().getSchemeSpecificPart(); if(action.equals(Intent.ACTION_PACKAGE_REMOVED)) { //卸载 }else if(action.equals(Intent.ACTION_PACKAGE_ADDED)) { //安装 } } };
相关文章推荐
- Android中使用GridView分页显示系统所安装的应用,支持拖动与手势滑动
- Android中使用GridView分页显示系统所安装的应用,支持拖动与手势滑动
- Android中使用GridView分页显示系统所安装的应用,支持拖动与手势滑动
- 【Android应用开发】-(18)静默方式实现批量安装卸载应用程序
- android应用打包成apk的两种方式
- android系统中,创建任意应用的快捷方式(shortcut)
- Android区分系统应用和安装应用
- android调用系统自带资源的两种方式
- Android获取系统已安装应用软件程序(apk)信息
- android 调用系统其他已经安装的应用的方法
- windows系统下安装weblogic的两种方式之二:脚本方式安装
- 【Android应用开发】-(18)静默方式实现批量安装卸载应用程序
- Android获取系统应用及安装应用的权限列表
- Android中使用GridView分页显示系统所安装的应用,支持拖动与手势滑动
- android重新安装系统应用
- Android中使用GridView分页显示系统所安装的应用,支持拖动与手势滑动
- android 在应用的配置文件设置apk的安装路径,以下是安装到sdcard上的方式
- android 获取系统已经安装的应用
- Android应用实现安装外部apk
- [置顶] 【Android应用开发】-(18)静默方式实现批量安装卸载应用程序