Android静默安装和静默卸载
来源:互联网 发布:海洋cms模板安装教程 编辑:程序博客网 时间:2024/06/11 05:20
package com.example.install_test;
这里写代码片
import java.io.DataOutputStream;
import java.io.File;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
/**
* 描述: app安装操作
* @author SCL
* @version 创建时间: 2016年11月8日 下午16:31
*/
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权限 */private 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;}/** * 静默安装 */private 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();
/* // 申请su权限
Process process = Runtime.getRuntime().exec(“su”);
dataOutputStream = new DataOutputStream(process.getOutputStream());
// 执行pm install命令
String command = “pm install -r ” + apkPath + “\n”;
dataOutputStream.write(command.getBytes(Charset.forName(“utf-8”)));
dataOutputStream.flush();
dataOutputStream.writeBytes(“exit\n”);
dataOutputStream.flush(); */
process.waitFor();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/** * 静默卸载 */private 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;}private static boolean returnResult(int value){ // 代表成功 if (value == 0) { return true; } else if (value == 1) { // 失败 return false; } else { // 未知情况 return false; } }
}
package com.example.install_test;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;
/**
* 描述: MainActivity
* @author SCL
*/
public class MainActivity extends Activity {
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);}/** * 描述: 安装 * @param */public void click1(View view){ new Thread(){ public void run() { String path = Environment.getExternalStorageDirectory()+"/Name.apk"; if (ApkController.install(path, getApplicationContext())){ toast("安裝成功"); }else{ toast("安裝失败"); } }; }.start();}/** * 描述: 卸载 * @param */public void click2(View view){ new Thread(){ public void run() { if (ApkController.uninstall("com.example.name", getApplicationContext())){ toast("卸载成功"); }else{ toast("卸载失败"); } }; }.start();}/** * 描述: 启动 * @param */public void click3(View view){ if (ApkController.startApp("com.example.name","com.example.name.MainActivity")) { toast("启动成功"); }}public void toast(final String text){ runOnUiThread(new Runnable() {重点内容 @Override public void run() { Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();; } });}
}
- Android静默安装和静默卸载
- Android--静默安装和静默卸载
- Android静默安装和静默卸载
- Android静默安装和静默卸载
- android实现静默卸载和静默安装
- Android静默安装和静默卸载
- Android静默安装和静默卸载
- 静默安装和静默卸载
- android 静默安装和卸载
- android静默安装和卸载
- Android 静默安装和卸载
- Android静默安装和静默卸载接口封装
- Android 静默安装和静默卸载(系统层)
- android静默安装、静默卸载apk方法
- android静默安装与静默卸载
- android apk静默安装和卸载
- android 实现静默安装和卸载
- android apk静默安装和卸载
- Markdown编辑器之Editor.md v1.5.0应用
- UVALive3902-Network(dfs)
- 使用Project Explorer的视图过滤器隐藏已关闭项目及显示父级空包
- 字典转json
- 36.211 Sidelink介绍 & 36.213 PSSCH,PSCCH
- Android静默安装和静默卸载
- Codeforces Round #378 (Div. 2) 前四题题解
- Spring+EhCache缓存实例(详细讲解+源码下载)
- 深度学习数据集汇总
- 日本語:日常会话6-食べ物について
- 模拟实现strstr函数
- 黑盒测试用例方法
- Linux里面的gcc编译程序游离问题
- Virtual Box配置网络以及SSH登陆配置