Android 安装应用的两种方式--外部应用安装器安装和静默安装(系统应用)
来源:互联网 发布:cpa难考吗 知乎 编辑:程序博客网 时间:2024/05/24 07:31
1.调用外部安装器安装
/** * 外部应用安装器安装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() { @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)) { //安装 } } };
阅读全文
0 0
- Android 安装应用的两种方式--外部应用安装器安装和静默安装(系统应用)
- Android静默安装应用
- Android静默安装应用
- Android静默安装应用
- Android--应用静默安装
- Android应用的静默安装
- android应用的安装之静默安装
- Android系统应用静默升级/安装
- Android系统应用静默升级/安装
- android应用静默安装方法
- 如何实现Android应用的静默安装
- 如何实现android应用的静默安装
- 如何实现Android应用的静默安装
- 如何实现Android应用的静默安装
- 如何实现Android应用的静默安装
- Android中实现应用的静默安装
- android应用后台安装,静默安装的代码实现方法
- android应用后台安装,静默安装的代码实现方法
- 数组指针和指针数组的区别
- iOS
- unity学习笔记#2--unity对象池
- FreeRTOS学习2--队列
- 世界地区选择
- Android 安装应用的两种方式--外部应用安装器安装和静默安装(系统应用)
- Eclipse servers插件安装
- 047、048、049-C++
- 端口与对应服务一览
- v4l2框架分析
- Mac安装mysql,忘记密码怎么办
- [Leetcode] 191. Number of 1 Bits 解题报告
- 自定义seekBar的样式
- KMP算法及字符串匹配学习