android 静默安装和卸载

来源:互联网 发布:网络名誉侵权纠纷法条 编辑:程序博客网 时间:2024/05/16 17:05

在android项目中经常会遇到需要静默安装和卸载的情况,特别是在升级的时候,为了提高用户体验感,避免多余的操作,会在项目中假如静默安装和卸载的功能,然后利用am命令执行指定的activity。还有就是通过把应用放在system/app的目录下也可以实现。


静默安装首先要判断用户是否有root权限,否则利用action来实现安装和卸载

package com.example.test;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import android.content.Context;import android.content.Intent;import android.net.Uri;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();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;}  }}

mainActivity

package com.example.test;import java.io.File;import android.support.v4.app.Fragment;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.Toast;import android.os.Build;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void click1(View view){new Thread(){public void run() {String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/jniTest.apk";if (ApkController.install(path, getApplicationContext())){toast("安裝成功");}else{toast("安裝失败");}};}.start();}public void click2(View view){new Thread(){public void run() {if (ApkController.uninstall("com.example.jnitest", getApplicationContext())){toast("卸載成功");}else{toast("卸載失败");}};}.start();}public void click3(View view){if (ApkController.startApp("com.example.jnitest","com.example.jnitest.MainActivity")) {toast("啟動成功");}}public void toast(final String text){runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();;}});}}


0 0
原创粉丝点击