赞
踩
因为是用命令行完成的,所以必须让应用获得root权限
Runtime.getRuntime().exec("su");
然后
主要是使用Android中的pm命令
pm install [-l] [-r] PATH
pm uninstall [-k] PACKAGE
Runtime.getRuntime().exec("命令");
参考:http://www.2cto.com/kf/201211/168926.html
有尝试过在Android手机里批量安装于批量卸载你的应用程序吗,现在就简单教你怎么做,其实很简单,还有后台安装于卸载问题:
1.后台安装于卸载步骤:
首先你需要使用其Pm命令,如一下
pm install [-l] [-r] [-t] [-i INSTALLER_PACKAGE_NAME] [-s] [-f] PATH
pm uninstall [-k] PACKAGE
代码实现也很简单,使用流的方式来进行相关操作:
public static void execCommand(String... command) {
02 Process process = null;
03 try {
04 process = new ProcessBuilder().command(command).start();
05 //对于命令的执行结果我们可以通过流来读取
06 // InputStream in = process.getInputStream();
07 // OutputStream out = process.getOutputStream();
08 // InputStream err = process.getErrorStream();
09 } catch (IOException e) {
10 e.printStackTrace();
11 } finally {
12 if (process != null)
13 process.destroy();
14 }
15 }
1 execCommand("pm", "install", "-f", filePath);//安装apk,filePath为apk文件路径,如/mnt/sdcard/ApiDemos.apk
2 execCommand("pm", "uninstall", packageName);//卸载apk,packageName为包名,如com.example.android.apis
注意一下:
编译生成apk时,要在你的manifest文件下添加android:sharedUserId="android.uid.system",编译完成之后还无法正常安装,会出现Installation error: INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误,此时,要为apk重新签名。
在android源码\build\target\product\security中找platform.pk8和platform.x509.pem两个文件,在android 编绎目录out下找到 signapk.jar 这个包(源码目录\build\tools\signapk),并把编译好的apk(如PMDemo.apk)放在同一目录下,在重新签名之前,用rar文件打开apk文件,进入META-INF目录下,将CERT.SF和CERT.RSA这两个文件删除掉,然后在命令行中执行以下命令:
1 java -jar signapk.jar platform.x509.pem platform.pk8 PMDemo.apk NewPMDemo.apk
安装前先把旧的apk卸载,这样重新签名之后的apk就可以正常安装了。
很简单吧,下面看下批量操作:
直接看代码吧:
通常情况下,android是没有提供静默方式的上层接口,我们需要在android源代码下来调用这个隐藏的接口来完成静默安装。
最重要的就是参考android系统目录下的packages/apps/PackageInstaller,
当中有两个文件 PackageInstallerActivity.java,InstallAppProgress.java ,前者就是我们通常看到的带有提示对话框的安装应用程序,后者是点确定安装后调用的intent。
现提供一个静默安装的关键类,该类在android2.2下成功编译, 其中通过循环调用接口instatllBatch则可实现批量安装
当然最后的应用程序别忘记添加权限
<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" />
package com.android.util;
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.content.pm.ApplicationInfo;
import android.content.pm.PackageParser;
import android.net.Uri;
import android.util.Log;
import android.util.DisplayMetrics;
import android.content.pm.IPackageInstallObserver;
import android.content.pm.IPackageDeleteObserver;
import android.os.FileUtils;
import android.os.Handler;
import android.os.Message;
public class PackageInstaller {
private File mTmpFile;
private final int INSTALL_COMPLETE = 1;
final static int SUCCEEDED = 1;
final static int FAILED = 0;
private final static String TAG = "PackInstaller";
private Context mContext;
private ApplicationInfo mAppInfo;
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) {
Log.i(TAG, "path=" + path);
int installFlags = 0;
Uri mPackageURI = Uri.fromFile(new File(path));
PackageParser.Package mPkgInfo = getPackageInfo(mPackageURI);
mAppInfo = mPkgInfo.applicationInfo;
String packageName = mAppInfo.packageName;
Log.i(TAG, "====install packageName ="+packageName);
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);
}
PackageInstallObserver observer = new PackageInstallObserver();
pm.installPackage(mPackageURI, observer, installFlags,
packageName);
}
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);
}
public PackageParser.Package getPackageInfo(Uri packageURI) {
final String archiveFilePath = packageURI.getPath();
PackageParser packageParser = new PackageParser(archiveFilePath);
File sourceFile = new File(archiveFilePath);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
PackageParser.Package pkg = packageParser.parsePackage(sourceFile,
archiveFilePath, metrics, 0);
// Nuke the parser reference.
packageParser = null;
return pkg;
}
public ApplicationInfo getApplicationInfo(Uri packageURI) {
final String archiveFilePath = packageURI.getPath();
PackageParser packageParser = new PackageParser(archiveFilePath);
File sourceFile = new File(archiveFilePath);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0);
if (pkg == null) {
return null;
}
return pkg.applicationInfo;
}
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case INSTALL_COMPLETE:
if(msg.arg1 == SUCCEEDED) {
} else {}
break;
default:
break;
}
}
};
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。