方式是将应用设置为内置的系统应用,注意事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;
}
}
}

最新文章

  1. zookeeper+dubbo-admin开发dubbo应用
  2. java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file:/XXX
  3. 怎样修改 Openstack Horizon(Dashboard)的显示界面 (一)
  4. lol 正在刷leetcode
  5. bootstrap学习笔记<八>(bootstrap核心布局风格——栅格系统)
  6. php excel (转)
  7. jquery easyui添加图标扩展
  8. Spring3 +mybatis3 之 MapperScannerConfigurer
  9. 规范javascript书写
  10. JavaScript代码性能优化总结
  11. Python标准库内置函数——hasattr
  12. 创建渐进式jpeg图片
  13. 第三章 AOP 编程选择
  14. SSL探03
  15. EJB(一)认识ejb
  16. php 便利数组方法
  17. Apache Curator入门实战
  18. MIPS 安全相关paper阅读笔记
  19. python设计模式之门面模式
  20. TopK排序

热门文章

  1. Linux搭建NFS提供磁盘给Windows使用
  2. winfrom 导入Excel表到access数据库(来自小抽奖系统)
  3. C# 执行JS
  4. 三个有用的SQL辅助工具
  5. “requireJs前传”之为什么要用前端模块化?
  6. Ubuntu开发环境搭建
  7. Sublime Text生成html标签快捷键
  8. Javascript 插件统一的实现步骤
  9. 压力测试之TCPP
  10. [转]Openwrt的Inittab