时间:2021-05-21
前段时间做了一个批量安装卸载应用程序的小应用,由于安装卸载应用程序的部分API是隐藏的,所以必须在ubuntu下下载Android系统源码,并编译之后使用MM命令编译生成APK文件,其实也难。
思路是这样的,在XX/packages/apps目录下有一个PackageInstaller的应用程序,Android机器中安装卸载都是由这个应用程序完成的。但是它没有批量安装和卸载的功能,如果要在自己的应用程序中添加批量安装和卸载的功能,其实很简单,只需要参考PakcageInstaller里面的安装卸载代码加个循环就可以了。但值得注意的是在编译的过程中必须复制PackageInstaller里面的Android.mk文件,修改文件为工程目录名。
好了,废话不再多说,下面是关键代码
1、 Android.mk文件
复制代码 代码如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := PackageInstaller
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)
复制代码 代码如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := PackageInstaller
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)
2、PakcageInstaller.java文件(关键代码)
复制代码 代码如下:
package cn.ceadic.apkmgr;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.util.Log;
import android.content.pm.IPackageInstallObserver;
import android.content.pm.IPackageDeleteObserver;
import android.os.FileUtils;
public class PackageInstaller {
private File mTmpFile;
private final String TMP_FILE_NAME = "tmpCopy.apk";
private final static String TAG = "PackInstaller";
private Context mContext;
public PackageInstaller(Context context) {
mContext = context;
}
public void install(String path,String packageName){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)),
"application/vnd.android.package-archive");
mContext.startActivity(intent);
}
public void instatllBatch(String path, String packageName) {
Log.i(TAG, "path=" + path);
int installFlags = 0;
PackageManager pm = mContext.getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
if (pi != null) {
installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
}
} catch (NameNotFoundException e) {
}
if ((installFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) {
Log.w(TAG, "Replacing package:" + packageName);
}
// Create temp file before invoking install api
mTmpFile = createTempPackageFile(path);
if (mTmpFile == null) {
// Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
// msg.arg1 = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
// mHandler.sendMessage(msg);
return;
}
Uri mPackageURI = Uri.parse("file://" + mTmpFile.getPath());
String installerPackageName = mContext.getIntent().getStringExtra(
Intent.EXTRA_INSTALLER_PACKAGE_NAME);
PackageInstallObserver observer = new PackageInstallObserver();
pm.installPackage(mPackageURI, observer, installFlags,
installerPackageName);
}
private File createTempPackageFile(String filePath) {
File tmpPackageFile = mContext.getFileStreamPath(TMP_FILE_NAME);
if (tmpPackageFile == null) {
Log.w(TAG, "Failed to create temp file");
return null;
}
if (tmpPackageFile.exists()) {
tmpPackageFile.delete();
}
// Open file to make it world readable
FileOutputStream fos;
try {
fos = openFileOutput(TMP_FILE_NAME, MODE_WORLD_READABLE);
} catch (FileNotFoundException e1) {
Log.e(TAG, "Error opening file " + TMP_FILE_NAME);
return null;
}
try {
fos.close();
} catch (IOException e) {
Log.e(TAG, "Error opening file " + TMP_FILE_NAME);
return null;
}
File srcPackageFile = new File(filePath);
if (!FileUtils.copyFile(srcPackageFile, tmpPackageFile)) {
Log.w(TAG, "Failed to make copy of file: " + srcPackageFile);
return null;
}
return tmpPackageFile;
}
private class PackageInstallObserver extends IPackageInstallObserver.Stub {
public void packageInstalled(String packageName, int returnCode) {
// Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
// msg.arg1 = returnCode;
// mHandler.sendMessage(msg);
Log.i(TAG, "====INSTALL_COMPLETE");
}
}
private class PackageDeleteObserver extends IPackageDeleteObserver.Stub {
public void packageDeleted(boolean succeeded) {
// Message msg = mHandler.obtainMessage(UNINSTALL_COMPLETE);
// msg.arg1 = succeeded?SUCCEEDED:FAILED;
// mHandler.sendMessage(msg);
Log.i(TAG, "====UNINSTALL_COMPLETE");
}
}
public void uninstall(String packageName){
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
packageURI);
mContext.startActivity(uninstallIntent);
}
public void uninstallBatch(String packageName) {
PackageDeleteObserver observer = new PackageDeleteObserver();
mContext.getPackageManager().deletePackage(packageName, observer, 0);
}
}
复制代码 代码如下:
package cn.ceadic.apkmgr;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.util.Log;
import android.content.pm.IPackageInstallObserver;
import android.content.pm.IPackageDeleteObserver;
import android.os.FileUtils;
public class PackageInstaller {
private File mTmpFile;
private final String TMP_FILE_NAME = "tmpCopy.apk";
private final static String TAG = "PackInstaller";
private Context mContext;
public PackageInstaller(Context context) {
mContext = context;
}
public void install(String path,String packageName){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)),
"application/vnd.android.package-archive");
mContext.startActivity(intent);
}
public void instatllBatch(String path, String packageName) {
Log.i(TAG, "path=" + path);
int installFlags = 0;
PackageManager pm = mContext.getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
if (pi != null) {
installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
}
} catch (NameNotFoundException e) {
}
if ((installFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) {
Log.w(TAG, "Replacing package:" + packageName);
}
// Create temp file before invoking install api
mTmpFile = createTempPackageFile(path);
if (mTmpFile == null) {
// Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
// msg.arg1 = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
// mHandler.sendMessage(msg);
return;
}
Uri mPackageURI = Uri.parse("file://" + mTmpFile.getPath());
String installerPackageName = mContext.getIntent().getStringExtra(
Intent.EXTRA_INSTALLER_PACKAGE_NAME);
PackageInstallObserver observer = new PackageInstallObserver();
pm.installPackage(mPackageURI, observer, installFlags,
installerPackageName);
}
private File createTempPackageFile(String filePath) {
File tmpPackageFile = mContext.getFileStreamPath(TMP_FILE_NAME);
if (tmpPackageFile == null) {
Log.w(TAG, "Failed to create temp file");
return null;
}
if (tmpPackageFile.exists()) {
tmpPackageFile.delete();
}
// Open file to make it world readable
FileOutputStream fos;
try {
fos = openFileOutput(TMP_FILE_NAME, MODE_WORLD_READABLE);
} catch (FileNotFoundException e1) {
Log.e(TAG, "Error opening file " + TMP_FILE_NAME);
return null;
}
try {
fos.close();
} catch (IOException e) {
Log.e(TAG, "Error opening file " + TMP_FILE_NAME);
return null;
}
File srcPackageFile = new File(filePath);
if (!FileUtils.copyFile(srcPackageFile, tmpPackageFile)) {
Log.w(TAG, "Failed to make copy of file: " + srcPackageFile);
return null;
}
return tmpPackageFile;
}
private class PackageInstallObserver extends IPackageInstallObserver.Stub {
public void packageInstalled(String packageName, int returnCode) {
// Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
// msg.arg1 = returnCode;
// mHandler.sendMessage(msg);
Log.i(TAG, "====INSTALL_COMPLETE");
}
}
private class PackageDeleteObserver extends IPackageDeleteObserver.Stub {
public void packageDeleted(boolean succeeded) {
// Message msg = mHandler.obtainMessage(UNINSTALL_COMPLETE);
// msg.arg1 = succeeded?SUCCEEDED:FAILED;
// mHandler.sendMessage(msg);
Log.i(TAG, "====UNINSTALL_COMPLETE");
}
}
public void uninstall(String packageName){
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
packageURI);
mContext.startActivity(uninstallIntent);
}
public void uninstallBatch(String packageName) {
PackageDeleteObserver observer = new PackageDeleteObserver();
mContext.getPackageManager().deletePackage(packageName, observer, 0);
}
}
3、别忘记添加权限
复制代码 代码如下:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />
复制代码 代码如下:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />
以上代码在Android2.1的SDK中编译通过,并正确批量安装卸载应用程序
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了android实现静默安装与卸载的方法。分享给大家供大家参考。具体如下:方法1:【使用调用接口方法,由于安装卸载应用程序的部分API是隐藏的,所以
在之前我们分析了Android6.0系统在启动时安装应用程序的过程,这些应用程序安装好之后,Launcher应用就负责把它们在桌面上展示出来。一、AMS启动La
Android退出应用程序的实现方法android退出应用程序会调用android.os.Process.killProcess(android.os.Proc
Android静默安装的方法,静默安装就是绕过安装程序时的提示窗口,直接在后台安装。注意:静默安装的前提是设备有ROOT权限。代码如下:/***静默安装*@pa
以应用程序为例,应用未安装的意思有: 1、有可能是手机内存不足导致应用程序安装不了,所以需要卸载一些应用程序或者删除一些文件。 2、很有可能是手机安装包有问