说明:2、3方法要添加系统权限和签名
android:sharedUserId="android.uid.system"
<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" />
1、反射
3、pm install命令
android:sharedUserId="android.uid.system"
<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" />
1、反射
package com.tnt.autoinstall;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
private Uri packageUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
packageUri = getPackageUri();
install();
}
private void install()
{
try {
Class<?> pmService;
Class<?> activityTherad;
Method method;
activityTherad = Class.forName("android.app.ActivityThread");
Class<?> paramTypes[] = getParamTypes(activityTherad , "getPackageManager");
method = activityTherad.getMethod("getPackageManager", paramTypes);
Object PackageManagerService = method.invoke(activityTherad);
pmService = PackageManagerService.getClass();
Class<?> paramTypes1[] = getParamTypes(pmService , "installPackage");
method = pmService.getMethod("installPackage", paramTypes1);
method.invoke(PackageManagerService , packageUri , null , 0 , null);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
}
private Class<?>[] getParamTypes(Class<?> cls, String mName) {
Class<?> cs[] = null;
Method[] mtd = cls.getMethods();
for (int i = 0; i < mtd.length; i++) {
if (!mtd[i].getName().equals(mName)) {
continue;
}
cs = mtd[i].getParameterTypes();
}
return cs;
}
private Uri getPackageUri()
{
File file = new File("/storage/sdcard0/download/1.apk");
return Uri.fromFile(file);
}
}
2、adb install 命令
private boolean install()
{
String[] args = {"pm", "install", "-r", packageUri.getPath()};
String result = null;
ProcessBuilder processBuilder = new ProcessBuilder(args);;
Process process = null;
InputStream errIs = null;
InputStream inIs = null;
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = -1;
process = processBuilder.start();
errIs = process.getErrorStream();
while((read = errIs.read()) != -1){
baos.write(read);
}
baos.write('\n');
inIs = process.getInputStream();
while((read = inIs.read()) != -1){
baos.write(read);
}
byte[] data = baos.toByteArray();
result = new String(data);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(errIs != null){
errIs.close();
}
if(inIs != null){
inIs.close();
}
}catch(Exception e){
e.printStackTrace();
}
if(process != null){
process.destroy();
}
}
if(result != null && (result.endsWith("Success")||result.endsWith("Success\n")))
{
return true;
}
return false;
}
3、pm install命令
private boolean install()
{
String[] args = {"pm", "install", "-r", packageUri.getPath()};
String result = null;
ProcessBuilder processBuilder = new ProcessBuilder(args);;
Process process = null;
InputStream errIs = null;
InputStream inIs = null;
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = -1;
process = processBuilder.start();
errIs = process.getErrorStream();
while((read = errIs.read()) != -1){
baos.write(read);
}
baos.write('\n');
inIs = process.getInputStream();
while((read = inIs.read()) != -1){
baos.write(read);
}
byte[] data = baos.toByteArray();
result = new String(data);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(errIs != null){
errIs.close();
}
if(inIs != null){
inIs.close();
}
}catch(Exception e){
e.printStackTrace();
}
if(process != null){
process.destroy();
}
}
if(result != null && (result.endsWith("Success")||result.endsWith("Success\n")))
{
return true;
}
return false;
}