android程序安装器,Android 安装应用的两种方式--外部应用安装器安装和静默安装(系统应用)...

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值