您的位置:首页 > 移动开发 > Android开发

android 实现静默安装和卸载

2013-03-25 10:18 267 查看
通过获取系统root权限调用pm install -r apkpath 和pm uninstall pkgname 来实现.
附上代码:
// install apk
private static void install(Activity activity, String path) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)),
"application/vnd.android.package-archive");
activity.startActivity(intent);
}

// uninstall a apk
private static void uninstall(Activity activity, String pkg) {
if (pkg == null) {
return;
}
Uri uri = Uri.fromParts("package", pkg, null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
activity.startActivity(intent);
}

// install
public static void install_root(final Activity activity,final String path, final int msgwhat, final Handler callback) {
new Thread() {
public void run() {
Process process = null;
OutputStream out = null;
InputStream in = null;
try {
// 请求root
process = Runtime.getRuntime().exec("su");
if(callback!=null){
Message msg = new Message();
msg.what = msgwhat;
msg.obj = 0; //安装开始
callback.sendMessage(msg);
}
out = process.getOutputStream();
// 调用安装
out.write(("pm install -r " + path + "\n").getBytes());
in = process.getInputStream();
byte[] bs = new byte[256];
int len = in.read(bs);
if (-1 != len) {
//取得root成功
String state = new String(bs, 0, len);
SLog.e("test", "state = "+state);
if (state.equals("Success\n")) {
if(callback!=null){
Message msg = new Message();
msg.what = msgwhat;
msg.obj = 1; //安装成功
callback.sendMessage(msg);
}
}else {
if(callback!=null){
Message msg = new Message();
msg.what = msgwhat;
msg.obj = 2; //安装失败
callback.sendMessage(msg);
}
}
}else{
//取得root失败
install(activity,path);
}

} catch (IOException e) {
SLog.e("test", "IOException 1111 = ");
e.printStackTrace();
} catch (Exception e) {
SLog.e("test", "Exception 1111 = ");
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}

public static void uninstall_root(final Activity activity,final String pkg,final int msgwhat, final Handler callback) {
new Thread() {
public void run() {
Process process = null;
OutputStream out = null;
InputStream in = null;
try {
// 请求root
process = Runtime.getRuntime().exec("su");
if(callback!=null){
Message msg = new Message();
msg.what = msgwhat;
msg.obj = 0; //卸载开始
callback.sendMessage(msg);
}
SLog.e("test", "111");
out = process.getOutputStream();
SLog.e("test", "222");
// 调用卸载
out.write(("pm uninstall " + pkg + "\n").getBytes());
SLog.e("test", "333");
in = process.getInputStream();
SLog.e("test", "444");
byte[] bs = new byte[256];
int len = in.read(bs);
SLog.e("test", "555 len = "+len);
if(-1 != len) {
//取得root成功
String state = new String(bs, 0, len);
SLog.e("test", "111 state = "+state);
if (state.equals("Success\n")) {
if(callback!=null){
Message msg = new Message();
msg.what = msgwhat;
msg.obj = 1; //卸载成功
callback.sendMessage(msg);
}
}else {
if(callback!=null){
Message msg = new Message();
msg.what = msgwhat;
msg.obj = 2; //卸载失败
callback.sendMessage(msg);
}
}
}else {
//取得root失败
uninstall(activity,pkg);
}

} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: