Android报错:Cannot run program “su “: error=13, Permission denied

一般的安装apk的方式

通过发一个Intent,把应用所在的路径封装整uri,之后默认启动了PackageInstaller.apk来安装程序了。

但是此种情况下,仅仅是个demo而已,很难达到开发者的需求。如:

1).界面不好

2).被用户知晓

3).什么时候安装完了,卸载完了呢?

第三点可以通过监听系统的安装/卸载的广播来实现

监听系统发出的安装广播

在安装和卸载完后,android系统会发一个广播

android.intent.action.PACKAGE_ADDED(安装)

android.intent.action.PACKAGE_REMOVED(卸载)

咱们就监听这广播,来做响应的逻辑处理。实现代码:

public class MonitorSysReceiver extends BroadcastReceiver{  
        
    @Override    
    public void onReceive(Context context, Intent intent){  
        //接收安装广播   
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {     
            //TODO      
        }     
        //接收卸载广播    
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {     
            //TODO  
        }  
    }  
} 

Android静默安装

Android静默安装,需要root权限,然后执行安装命令:

 pm install -r packageName

静默安装主要分为以下几种方式:

一、在ROOT过的机器上,在App中使用pm install指令安装APK:

二、修改系统应用PackageManagerIntaller.apk的源码,增加无界面的安装接口:

三、通过反射调用PackageManager.java中的隐藏API来实现静默安装:

Android中实现静态的默认安装和卸载应用

Android应用如何获取root权限

在已经root过得手机上获取root权限,简单
Java代码:

Process process = Runtime.getRuntime().exec("su");

su命令没有权限使用

以下内容,参考自:https://blog.csdn.net/kangear/article/details/51872653.

Android4.2.2(Jelly Bean)上,调用su命令就可以获取到root权限并执行一些命令。但是在Android4.3之上的版本,Google为这种获取root权限的方法设置了层层障碍:

1.su命令源码中添加了uid检验,只允许shell/root用户进行调用;

2.Zygote源码中添加了一些东西,屏蔽掉了App可以进行setuid的功能;

3.adb源码中添加了一些东西,屏蔽掉了adb可以进行setuid的功能(这里指的是对于非userdebug等类似版本的系统) ;

4.开启了SELinux安全模块,1和2条都满足情况下也会中断su命令的执行.

Android app如何获取root权限

一般来说, Android 下的应用程序可以直接得到的最大的权限为 system ,但是如果我们需要在程序中执行某些需要 root 权限的命令,如 ifconfig 等,就需要 root 权限了。

两种安装方式

1. root权限静默安装实现

使用的是su pm install -r filePath命令。
代码如下:

protected static void excuteSuCMD() { 
   Process process = null; 
   OutputStream out = null; 
   InputStream in = null; 
   String currentTempFilePath = "/sdcard/QQ.apk"; 
   try { 
   // 请求root 
   process = Runtime.getRuntime().exec("su"); 
   out = process.getOutputStream(); 
   // 调用安装 
   out.write(("pm install -r " + currentTempFilePath + "\n").getBytes()); 
   in = process.getInputStream(); 
   int len = 0; 
   byte[] bs = new byte[256]; 
   while (-1 != (len = in.read(bs))) { 
   String state = new String(bs, 0, len); 
   if (state.equals("Success\n")) { 
    //安装成功后的操作 
     } 
    } 
   } 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(); 
    } 
   } 
  }

2. 非root权限提示用户安装

代码如下:

public static void openFile() { 
  // 核心是下面几句代码 
  if (!isHasfile()) { 
   downLoadFile(url); 
  } 
  Intent intent = new Intent(); 
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
  intent.setAction(android.content.Intent.ACTION_VIEW); 
  intent.setDataAndType( 
    Uri.fromFile(new File("/sdcard/update/updata.apk")), 
    "application/vnd.android.package-archive"); 
  mContext.startActivity(intent); 
 }

参考:
Android:java.io.IOException: Cannot run program “/system/xbin/su”: error=13, Permission denied

Cannot run program "su ": error=13, Permission denied in Android Application

Android中实现静态的默认安装和卸载应用
Android静默安装的实现方案(一)
Android实现静默安装

android apk无法安装高版本号,android 使用FileProvider 兼容apk 在7.0版本无法安装

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值