注意:
1 如果这个apk是系统应用( /system/app/ ),那么使用这种方式进行更新版本时,会在 /data/app/ 下面安装apk包,更新后的应用可以卸载(adb卸载),一旦卸载/data/app目录下的APK,在桌面上显示的就是原来的系统应用( /system/app/ )。对这个系统应用进行多次更新,更新后均会在/data/app目录下,而之前的包会被替换掉,在这个目录只会有一个该应用的包。
2 如果apk是系统应用( /system/app/ ),当安装后,桌面上依旧不可长按直接卸载!
public class MainActivity extends Activity {
Button btnInstatll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnInstatll = (Button)findViewById(R.id.btnInstatll);
btnInstatll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//String str = Environment.getExternalStorageDirectory()+"/Test/TestMain.apk";
String str = Environment.getRootDirectory()+"/system";
//String str = Environment.getExternalStorageDirectory()+"/Test/TestMain.apk";
boolean perssion = getRootPermission(str);
Log.d("[hhb]", "perssion = "+perssion);
if(perssion)
{
String strPatch = Environment.getExternalStorageDirectory()+"/Test/TestSetUp.apk";
Log.d("[hhb]", "Environment = "+strPatch);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(strPatch)),
"application/vnd.android.package-archive");
MainActivity.this.startActivity(intent);
}
}
});
}
public boolean getRootPermission(String pkgCodePath) {
Log.d("[hhb]", "pkgCodePath=" + pkgCodePath);
Process process = null;
DataOutputStream os = null;
try {
String cmd = "chmod 777 " + pkgCodePath;
//String mount = "mount -o remount rw /system";
process = Runtime.getRuntime().exec("su"); //切换到root帐号
os = new DataOutputStream(process.getOutputStream());
//os.writeBytes(mount + "\n");
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush(); //表示强制将缓冲区中的数据发送出去,不必等到缓冲区满
process.waitFor();
} catch (Exception e) {
Toast.makeText(this, "root error!" + pkgCodePath, Toast.LENGTH_SHORT).show();
e.printStackTrace();
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
Toast.makeText(this, "root success!" + pkgCodePath, Toast.LENGTH_SHORT).show();
return true;
}
}