SD卡安装APK 获取root权限安装2014.10.04

注意:

   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;  
    }  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自动驾驶学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值