PackageManager分析(5)
pakcage install 示例。
如果有哪些代码不明白,可以留言给我,我再解释。
public class InstallThread extends Thread { //因为安装时间比较耗时,一般放在单独的线程中
public static final int INSTALL_SUCCESS = 1;
public static final int INSTALL_FAIL = 0;
public static final int INSTALL_MSG_WHAT = 0;
private int mInstallFlags = 0;
private int mInstallResult = 1;
private Uri mInstallFileUri = null;
private PackageManager mPackageManager;
private String mFileName;
private Context mContext;
private Handler mHandler;
private String mType;
private String mId;
public InstallThread(Context context, Handler handler, String type, String id) {
mContext = context;
mHandler = handler;
mType = type;
mId = id;
}
@Override
public void run() {
try {
mPackageManager = mContext.getPackageManager();
mInstallFlags = getInstallFlags();
mInstallFileUri = findApk();
PackageInstallObserver observer = new PackageInstallObserver();
mPackageManager.installPackage(mInstallFileUri, observer, mInstallFlags, null);
} catch (Exception e) {
e.printStackTrace();
if (mHandler != null) {
Message msg = mHandler.obtainMessage(INSTALL_MSG_WHAT);
msg.arg1 = INSTALL_FAIL;
msg.obj = mFileName;
mHandler.sendMessage(msg);
}
}
}
private class PackageInstallObserver extends IPackageInstallObserver.Stub {
public void packageInstalled(String packageName, int returnCode) {
try {
if(checkInstallResult(returnCode)) {
mInstallResult = INSTALL_SUCCESS;
} else {
mInstallResult = INSTALL_FAIL;
}
if (mHandler != null) {
Message msg = mHandler.obtainMessage(INSTALL_MSG_WHAT);
msg.arg1 = mInstallResult;
msg.obj = mFileName;
mHandler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private Uri findApk() {
LocalItem item = LocalDataManager.getInstance().query(mContext, mContext.getContentResolver(), mType, mId);
String path = Config.getFilePath(item.getType(), item.getFileName());
mFileName = item.getFileName();
Uri uri = Uri.parse(path);
Log.d(TAG, "apk file uri ================== " + uri.toString());
return uri;
}
private int getInstallFlags() {
int flags = 0;
if (Config.getIsInstallReplace()) {
flags |= PackageManager.INSTALL_REPLACE_EXISTING;
}
if (Config.getIsInstallExternal()) {
flags |= PackageManager.INSTALL_EXTERNAL;
} else {
flags |= PackageManager.INSTALL_INTERNAL;
}
return flags;
}
private boolean checkInstallResult(int returnCode) {
Log.d(TAG, " return code ==================== " + returnCode);
switch(returnCode) {
case PackageManager.INSTALL_SUCCEEDED: //1
Log.d(TAG, "install success!");
return true;
//data partition error
case PackageManager.INSTALL_FAILED_ALREADY_EXISTS: //-1
Log.d(TAG, "install failed: package already exists");
break;
case PackageManager.INSTALL_FAILED_INVALID_APK: //-2
Log.d(TAG, "install failed: invalid apk file");
break;
case PackageManager.INSTALL_FAILED_INVALID_URI: //-3
Log.d(TAG, "install failed: passed invalid uri ");
break;
case PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE: //-4
Log.d(TAG, "install failed: didn't have enough space ");
break;
case PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE: //-5
Log.d(TAG, "install failed: already installed with the same name");
break;
case PackageManager.INSTALL_FAILED_NO_SHARED_USER: //-6
Log.d(TAG, "install failed: requested shared user does not exist ");
break;
case PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE: //-7
Log.d(TAG, "install failed: previously installed package of the same name has a different signature ");
break;
case PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE: //-8
Log.d(TAG, "install failed: requested a shared user which is already installed on the device and does not have matching signature ");
break;
case PackageManager.INSTALL_FAILED_MISSING_SHARED_LIBRARY: //-9
Log.d(TAG, "install failed: package uses a shared library that is not available ");
break;
case PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE: //-10
Log.d(TAG, "install failed: replace couldn't delete ");
break;
case PackageManager.INSTALL_FAILED_DEXOPT: //-11
Log.d(TAG, "install failed: optimizing and validating its dex files failed ");
break;
case PackageManager.INSTALL_FAILED_OLDER_SDK: //-12
Log.d(TAG, "install failed: current SDK version is older than that required by the package ");
break;
case PackageManager.INSTALL_FAILED_CONFLICTING_PROVIDER: //-13
Log.d(TAG, "install failed: a provider already installed ");
break;
case PackageManager.INSTALL_FAILED_NEWER_SDK: //-14
Log.d(TAG, "install failed: current SDK version is newer than that required by the package ");
break;
case PackageManager.INSTALL_FAILED_TEST_ONLY: //-15
Log.d(TAG, "install failed: it is a test-only package");
break;
case PackageManager.INSTALL_FAILED_CPU_ABI_INCOMPATIBLE: //-16
Log.d(TAG, "install failed: contains native code, but none that is compatible with the the device's CPU_ABI ");
break;
case PackageManager.INSTALL_FAILED_MISSING_FEATURE: //-17
Log.d(TAG, "install failed: package uses a feature that is not available ");
break;
//sdcard error
case PackageManager.INSTALL_FAILED_CONTAINER_ERROR: //-18
Log.d(TAG, "install failed: a secure container mount point couldn't be accessed on external media ");
break;
case PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION: //-19
Log.d(TAG, "install failed: the new package couldn't be installed in the specified install location ");
break;
case PackageManager.INSTALL_FAILED_MEDIA_UNAVAILABLE: //-20
Log.d(TAG, "install failed: the media is not available ");
break;
//parser error
case PackageManager.INSTALL_PARSE_FAILED_NOT_APK: //-100
Log.d(TAG, "install failed: a path that is not a file, or does not end with the expected '.apk' extension");
break;
case PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST: //-101
Log.d(TAG, "install failed: the parser was unable to retrieve the AndroidManifest.xml file");
break;
case PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION: //-102
Log.d(TAG, "install failed: the parser encountered an unexpected exception");
break;
case PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES: //-103
Log.d(TAG, "install failed: parser did not find any certificates in the .apk ");
break;
case PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES: //-104
Log.d(TAG, "install failed: parser found inconsistent certificates on the files in the .apk");
break;
case PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING: //-105
Log.d(TAG, "install failed: parser encountered a CertificateEncodingException");
break;
case PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME: //-106
Log.d(TAG, "install failed: parser encountered a bad or missing package name in the manifest ");
break;
case PackageManager.INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID: //-107
Log.d(TAG, "install failed: parser encountered a bad shared user id name in the manifest ");
break;
case PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED: //-108
Log.d(TAG, "install failed: parser encountered some structural problem in the manifest ");
break;
case PackageManager.INSTALL_PARSE_FAILED_MANIFEST_EMPTY: //-109
Log.d(TAG, "install failed: parser did not find any actionable tags (instrumentation or application) in the manifest");
break;
case PackageManager.INSTALL_FAILED_INTERNAL_ERROR: //-110
Log.d(TAG, "install failed: the system failed to install the package because of system issues ");
break;
default:
Log.d(TAG, "install failed: unknow reason ");
break;
}
return false;
}
}
相关推荐
在Android开发中,`PackageManager`是一个至关重要的组件,它提供了获取和管理安装在设备上的所有应用包信息的能力。本文将详细讲解如何利用`PackageManager`来获取Android应用程序(包)的相关信息。 首先,让我们...
在Android系统中,`PackageManager`是一个非常重要的服务组件,它负责管理手机上所有已安装的应用程序。通过`PackageManager`,开发者可以获取到设备上的应用信息,包括应用的名称、图标、权限、版本号等。本篇文章...
Android PackageManager 有很多方法是没有暴漏出来的,我们如果想调用的话需要用Java反射。android_dependency.jar 提供了反射调用相关的class定义,直接放到你的工程libs目录下面,然后就可以进行反射调用...
在Android开发中,`PackageManager` 是一个至关重要的组件,它提供了获取系统中所有安装应用信息的能力,包括应用的包名、名称、图标等。本文将深入探讨如何利用 `PackageManager` 获取系统应用包名并实现根据包名...
在Android系统中,`PackageManager`是一个非常重要的服务组件,它负责管理手机上所有安装的应用程序。通过`PackageManager`,开发者可以获取到应用的基本信息,包括应用的名称、图标、版本号、权限等,并且能够实现...
Android PackageManager 有很多方法是没有暴漏出来的,我们如果想调用的话需要用Java反射。android_dependency.jar 提供了反射调用相关的class定义,直接放到你的工程libs目录下面,然后就可以进行反射调用...
在Android系统中,PackageManager服务是应用开发者和系统交互的核心组件之一。它负责管理设备上的所有应用程序,包括安装、卸载、查询以及获取应用程序的各种信息。本篇将详细讲解`PackageManager`中的`...
【Laravel开发-packagemanager】是一个专为Laravel框架设计的工具,旨在简化开发者在创建新模块时的工作流程。Laravel作为一个优雅的PHP框架,强调了代码的清晰度和可维护性,而packagemanager正是在这个基础上,...
【Android_PackageManager分析】 Android的PackageManager服务是系统的核心组件之一,它负责管理应用程序的安装、卸载以及应用信息的查询等操作。在Android系统中,PackageManager涉及到的主要知识点包括: 1. **`...
### Android中获取应用程序(包)的信息—PackageManager的使用方法 #### 概述 在Android开发过程中,有时我们需要获取系统中应用程序的信息,例如包名(`packagename`)、标签(`label`)、图标(`icon`)以及占用...
本文将详细讲解如何通过Android的`PackageManager`服务来获取应用程序的大小,以及背后的实现原理。 首先,我们需要理解`PackageManager`在Android中的角色。`PackageManager`是Android系统的一个核心组件,它负责...
可根据包名打开应用程序(不算系统的)MainActivity里面的getLaunchIntentForPackage里面的("com.wandoujia.phoenix2")需要修改,这里我是用的是豌豆荚的包名,如果没装豌豆荚的请安装,或者包名参数改成你自己的...
在Android开发中,`PackageManager` 是一个至关重要的组件,它为开发者提供了管理和操作应用程序的能力。通过`PackageManager`,我们可以获取到设备上安装的所有应用的信息,包括应用的基本元数据、权限、服务、活动...
在Android系统中,PackageManager服务是Android应用程序框架的重要组成部分,它主要负责管理设备上的所有应用包。这个服务提供了安装、卸载、查询、获取权限等多种功能,使得开发者能够方便地操作和管理应用程序。...
本文实例讲述了Android实现整理PackageManager获取所有安装程序信息的方法。分享给大家供大家参考,具体如下: List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);//获取安装程序的包名 for ...
List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES); ``` 这里的`GET_UNINSTALLED_PACKAGES`标志用于包含已卸载的应用信息(如果权限允许)。 2. **...
List<ApplicationInfo> installedApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA); ``` `getInstalledApplications()`方法返回一个`ApplicationInfo`对象的列表,每个`...
首先,需要`PackageManager`对象,然后调用`getPackageInfo(packageName, 0)`方法,传入目标包名,`0`表示我们不需要额外的信息。返回的`PackageInfo`对象的`packageName`字段就是包名。 **获取启动类名:** 启动类...
您现在可以解压缩PackageManager.zip并进入PackageManager文件夹配置应用您可以在config/package-manager.yml找到配置文件localRepo这是包含所有依赖项的文件列表的文件夹的路径gitRepo这是远程git存储库的URL,...