1.普通的应用安装和卸载
[java] view plain copy
- /* 安装apk */
- public static void installApk(Context context, String fileName) {
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_VIEW);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setDataAndType(Uri.parse("file://" + fileName),"application/vnd.android.package-archive");
- context.startActivity(intent);
- }
- /* 卸载apk */
- public static void uninstallApk(Context context, String packageName) {
- Uri uri = Uri.parse("package:" + packageName);
- Intent intent = new Intent(Intent.ACTION_DELETE, uri);
- context.startActivity(intent);
- }
监听系统发出的安装广播
在安装和卸载完后,android系统会发一个广播
android.intent.action.PACKAGE_ADDED(安装)
android.intent.action.PACKAGE_REMOVED(卸载)
我们可以监听这两个广播,来进行相应的处理
[java] view plain copy
- public class MonitorSysReceiver extends BroadcastReceiver{
- @Override
- public void onReceive(Context context, Intent intent){
- //接收安装广播
- if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
- //TODO
- }
- //接收卸载广播
- if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
- //TODO
- }
- }
- }
[java] view plain copy
- <receiver android:name=".MonitorSysReceiver">
- <intent-filter>
- <action android:name="android.intent.action.PACKAGE_ADDED" />
- <action android:name="android.intent.action.PACKAGE_REMOVED" />
- </intent-filter>
- </receiver>
2.Android的静默安装和卸载
Android的静默安装和卸载,需要将应用注册成系统用户组,并且需要在系统源码下编译才行。
[html] view plain copy
- android:sharedUserId="android.uid.system"
[html] view plain copy
- <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
- <uses-permission android:name="android.permission.DELETE_PACKAGES" />
静默安装
安装方法:
[java] view plain copy
- String fileName = Environment.getExternalStorageDirectory() + File.separator + "baidu"+File.separator +"UC.apk";
- Uri uri = Uri.fromFile(new File(fileName));
- int installFlags = 0;
- PackageManager pm = getPackageManager();
- try {
- PackageInfo pi = pm.getPackageInfo("com.UCMobile",PackageManager.GET_UNINSTALLED_PACKAGES);
- if(pi != null) {
- installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
- }
- } catch (Exception e) {
- Log.e("exception",e.toString());
- }
- MyPakcageInstallObserver observer = new MyPakcageInstallObserver();
- pm.installPackage(uri, observer, installFlags, "com.UCMobile");
安装回调
[java] view plain copy
- /*静默安装回调*/
- class MyPakcageInstallObserver extends IPackageInstallObserver.Stub{
- @Override
- public void packageInstalled(String packageName, int returnCode) {
- if (returnCode == 1) {
- Log.e("DEMO","安装成功");
- new ToastThread(InstallActivity.this,"安装成功").start();
- }else{
- Log.e("DEMO","安装失败,返回码是:"+returnCode);
- new ToastThread(InstallActivity.this,"安装失败,返回码是:"+returnCode).start();
- }
- }
- }
静默卸载:
[java] view plain copy
- PackageManager pm = ParentManagementSettings.this.getPackageManager();
- IPackageDeleteObserver observer = new MyPackageDeleteObserver();
- pm.deletePackage("com.readboy.microclassphone", observer, 0);
[html] view plain copy
- /**
- * 应用静默删除回调
- * @author 1
- *
- */
- class MyPackageDeleteObserver extends IPackageDeleteObserver.Stub {
- @Override
- public void packageDeleted(String packageName, int returnCode) {
- if (returnCode == 1) {
- Log.e("DEMO","success...");
- }else{
- Log.e("DEMO","fail code:"+returnCode);
- }
- }
- }
备注:
根据APK的path路径,查看APK的包名
[java] view plain copy
- PackageManager pm2=getActivity().getPackageManager();
- PackageInfo info=pm2.getPackageArchiveInfo(files[i].getPath(), 0);
- Log.e("DEMO", "infoName"+info.packageName);
- packageNameString=info.packageName;
原文地址:https://blog.csdn.net/qwe380948730/article/details/54909308