方式是将应用设置为内置的系统应用,注意事system/app目录下面,采用copy2SystemApp()方法就可以,注意chmod 777的权限,若是直接将apk拷贝到system/app目录,没有这个权限还是不能静默安装的。

直接贴出工具类:

public class ApkController {
/**
* 描述: 安装
*/
public static boolean install(String apkPath,Context context){
// 先判断手机是否有root权限
if(hasRootPerssion()){
// 有root权限,利用静默安装实现
return clientInstall(apkPath);
}else{
// 没有root权限,利用意图进行安装
File file = new File(apkPath);
if(!file.exists())
return false;
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
context.startActivity(intent);
return true;
}
} /**
* 描述: 卸载
*/
public static boolean uninstall(String packageName,Context context){
if(hasRootPerssion()){
// 有root权限,利用静默卸载实现
return clientUninstall(packageName);
}else{
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,packageURI);
uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(uninstallIntent);
return true;
}
} /**
* 判断手机是否有root权限
*/
public static boolean hasRootPerssion(){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
} /**
* 静默安装
*/
public static boolean clientInstall(String apkPath){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.println("chmod 777 "+apkPath);
PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
PrintWriter.println("pm install -r "+apkPath);
// PrintWriter.println("exit");
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
} /**
* 静默卸载
*/
public static boolean clientUninstall(String packageName){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib ");
PrintWriter.println("pm uninstall "+packageName);
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
} /**
* 启动app
* com.exmaple.client/.MainActivity
* com.exmaple.client/com.exmaple.client.MainActivity
*/
public static boolean startApp(String packageName,String activityName){
boolean isSuccess = false;
String cmd = "am start -n " + packageName + "/" + activityName + " \n";
Process process = null;
try {
process = Runtime.getRuntime().exec(cmd);
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(process!=null){
process.destroy();
}
}
return isSuccess;
}
/**
* 将文件复制到system/app 目录
* @param apkPath 特别注意格式:该路径不能是:/storage/emulated/0/app/QDemoTest4.apk 需要是:/sdcard/app/QDemoTest4.apk
* @return
*/
public static boolean copy2SystemApp(String apkPath){
PrintWriter PrintWriter = null;
Process process = null;
String appName = "chetou.apk",cmd; try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
cmd = "mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system";
Log.e("copy2SystemApp", cmd);
PrintWriter.println(cmd); cmd = "cat "+apkPath+" > /system/app/"+appName;
Log.e("copy2SystemApp", cmd);
PrintWriter.println(cmd); cmd = "chmod 777 /system/app/"+appName +" -R";
Log.e("copy2SystemApp", cmd);
PrintWriter.println(cmd); cmd = "mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system";
Log.e("copy2SystemApp", cmd);
PrintWriter.println(cmd);
PrintWriter.println("reboot"); //重启
PrintWriter.println("exit");
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
private static boolean returnResult(int value){
// 代表成功
if (value == 0) {
return true;
} else if (value == 1) { // 失败
return false;
} else { // 未知情况
return false;
}
}
}

android开发实现静默安装(root权限)的更多相关文章

  1. android开发实现静默安装(fota升级)

    这里只提供一个思路,也是咨询大神才了解到的. fota升级主要用于系统及系统应用的升级,不过貌似也会弹出提示用于用户确认.既然做到系统级别了,估计也一样可以静默安装的.

  2. Android随笔之——静默安装、卸载

    随笔之所以叫随笔,就是太随意了,说起来,之前的闹钟系列随笔还没写完,争取在十月结束之前找时间把它给写了吧.今天要讲的Android APK的静默安装.卸载.网上关于静默卸载的教程有很多,更有说要调用隐 ...

  3. Android从零开始——Android开发环境的安装

    Android开发环境的安装 1 IDE Android可以使用开发的IDE有Eclipse 或者 Android Studio.Android Studio还处于v 0.1.x版本,是early a ...

  4. Android开发环境的安装 Eclipse

    Android开发环境的安装 1 IDE Android可以使用开发的IDE有Eclipse 或者 Android Studio.Android Studio还处于v 0.1.x版本,是early a ...

  5. android开发 更新升级安装到一半自动闪退

    如题:android开发 更新升级安装到一半自动闪退,,,解决办法,如下(红色为我新增的代码) /**     * 安装APK文件     */    private void installApk( ...

  6. 【Android】Android 代码判断是否获取ROOT权限(二)

    [Android]Android 代码判断是否获取ROOT权限 方法比较简单,直接粘贴代码 /** * 判断当前手机是否有ROOT权限 * @return */ public boolean isRo ...

  7. 【Android】Android 代码判断是否获取ROOT权限(一)

    [Android]Android 代码判断是否获取ROOT权限 方法比较简单,直接粘贴代码 public synchronized boolean getRootAhth() { Process pr ...

  8. android开发,关于android app实现静默安装自己(系统签名)

    产品需求,木有办法.android系统是跟厂商定制的,保证系统开机就运行我们的app,并且实现自己静默安装,完全自动化,无需人工操作. 网上有很多办法, 1.要么要通过android 源码拿到密钥文件 ...

  9. Android公共库(缓存 下拉ListView 下载管理Pro 静默安装 root运行 Java公共类)

    介绍总结的一些android公共库,包含缓存(图片缓存.预取缓存).公共View(下拉及底部加载更多ListView.底部加载更多ScrollView.滑动一页Gallery).及Android常用工 ...

随机推荐

  1. 基本的Web控件二

    ListBox控件 ListBox控件用于创建多选的列表框,而可选项是通过ListItem元素来定义的. ListBox控件常用的属性: 1.Count:表示列表框中条目的总数. 2.Items:表示 ...

  2. ios球体弹跳游戏源码

    一款耐玩的ios游戏源码,画面上有很多小星星,球体落下的时候,你需要在画面上画出一条条的线条让球体弹跳起来然后吃掉小星星,如果没借助球体就失败了.游戏有很多关卡.注意: <ignore_js_o ...

  3. sublime text2支持ng

    这里面记录了sublime text3的一些破解和sublime text2支持ng的方法. http://weblogs.asp.net/dwahlin/archive/2013/08/30/usi ...

  4. SqlServer将表中数据复制到另一张表

    insert into phone2(ph,attr,type,carrier) select top 1000 ph,attr,type,carrier from phone 将表phone的字段和 ...

  5. EasyUI datagrid checkbox数据设定与取值(转自http://blog.csdn.net/baronyang/article/dnetails/9323463,感谢分享,谢谢)

    这一篇将会说明两种使用 jQuery EasyUI DataGrid 的 Checkbox 设定方式,以及在既有数据下将 checked 为 true 的该笔数据列的 Checkbox 设定为 Che ...

  6. C# Async与Await用法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. WordPress 主题开发 - (四) 创建WordPress的主题HTML结构 待翻译

    Now we're starting to get into the real meat of WordPress Theme development: coding the HTML structu ...

  8. mysql开启全文索引功能

    注意:全文检索模块需要mysql开启全文索引功能,开启方法:修改mysql配置文件:window服务器为my.ini,linux服务器为my.cnf,在 [mysqld] 后面加入一行“ft_min_ ...

  9. Simple Shopping Cart By AngularJS

    <body ng-controller='CartController'> <h1>Your Order</h1> <div ng-repeat='item ...

  10. angularjs2 学习笔记(五) http服务

    angular2的http服务是用于从后台程序获取或更新数据的一种机制,通常情况我们需要将与后台交换数据的模块做出angular服务,利用http获取更新后台数据,angular使用http的get或 ...