- 关键词:Android,源码,卸载,app,apk。
- 注:目前先记录调用链,详细代码以后有空补上。
在代码中卸载app的方式:
发送的intent(ACTION_UNINSTALL_PACKAGE),
来调用UninstallerActivity.java。
代码:
String packageName = "xxx.xxx.xxx.xxx";//要卸载的应用包名
PackageManager pm = cxt.getPackageManager();//获取包管理器
List<PackageInfo> pinfo = pm.getInstalledPackages(0);//获取已安装包名的list
for (int i = 0; i < pinfo.size(); i++) {//遍历
if (pinfo.get(i).packageName.equalsIgnoreCase(packageName)) {
//判断 要卸的包 是否存在
Intent uninstall_intent = new Intent();
uninstall_intent.setAction(Intent.ACTION_DELETE);//设置action为卸载已安装的包
uninstall_intent.setData(Uri.parse("package:" + packageName));//设置
startActivity(uninstall_intent);
}
}
- UninstallerActivity.java 在 AndroidManifest.xml 的配置
<activity android:name=".UninstallerActivity"
<action android:name="android.intent.action.DELETE" />
<action android:name="android.intent.action.UNINSTALL_PACKAGE" />
- 进入UninstallerActivity.java
--> onCreate()
--> showConfirmationDialog() //在此判断是否system/
--> showDialogFragment(new UninstallAlertDialogFragment())
-->> UninstallAlertDialogFragment$$onCreateDialog() // 去新的类,内部类
- 进入UninstallerActivity.java
--> onCreateDialog(Bundle savedInstanceState)
//弹窗显示:要卸载此应用吗(资源名:uninstall_application_text)
//点击确定后,
-->> ((UninstallerActivity) getActivity()).startUninstallProgress();// 去外部类
- 进入UninstallerActivity.java
--> startUninstallProgress()
--> newIntent.setClass(this, UninstallAppProgress.class);
-->> startActivity(newIntent);// 去新的类
- 进入UninstallAppProgress.java
- 如果静默卸载,就直接从这一步开始
--> onCreate
--> getPackageManager().deletePackageAsUser(mAppInfo.packageName, observer, mAllUsers ? PackageManager.DELETE_ALL_USERS : 0, mUser.getIdentifier());
// 去新的类
// PackageManagerService.java$$deletePackageAsUser
- PackageManagerService.java
//调用链
--> deletePackageAsUser
--> deletePackage
--> deletePackageX
--> (4)deletePackageLIF --
--> deleteInstalledPackageLIF
--> (6)removePackageDataLIF
--> (7)removePackageLI
--> cleanPackageDataStructuresLILPw
配合log来看:
10-18 03:00:32.044 769 1125 D PackageManager: deletePackageAsUser: pkg=com.topwise.httpdemo user=0 deleteAllUsers: false
10-18 03:00:32.055 769 869 D PackageManager: deletePackageX: pkg=com.topwise.httpdemo user=0
10-18 03:00:32.056 769 869 D PackageManager: (4)deletePackageLI: com.topwise.httpdemo user UserHandle{0}
10-18 03:00:32.057 769 869 D PackageManager: Marking package:com.topwise.httpdemo uninstalled for user:0
10-18 03:00:32.059 769 869 D PackageManager: Not installed by other users, full delete
10-18 03:00:32.059 769 869 D PackageManager: Removing non-system package: com.topwise.httpdemo
10-18 03:00:32.060 769 832 I ActivityManager: Force stopping com.topwise.httpdemo appid=10040 user=0: deletePackageX
10-18 03:00:32.060 769 869 D PackageManager: (6)removePackageDataLI: PackageSetting{99cd254 com.topwise.httpdemo/10040}
10-18 03:00:32.060 769 869 D PackageManager: (7)Removing package com.topwise.httpdemo
10-18 03:00:32.060 769 869 D PackageManager: Unregistered content provider: com.topwise.httpdemo.com.android.tools.ir.server.InstantRunContentProvider, className = com.android.tools.ir.server.InstantRunContentProvider,
isSyncable = false
10-18 03:00:32.060 769 869 D PackageManager: Providers: com.android.tools.ir.server.InstantRunContentProvider
10-18 03:00:32.060 769 869 D PackageManager: Activities: com.topwise.httpdemo.MainActivity
10-18 03:00:32.103 769 1575 W PackageManager: checkUidPermission(): android.permission.STATUS_BAR of 10025 is denied.
网友评论