静默顾名思义就是静静的默默地,静默安装和静默卸载的意思也就是说在后台默默地安装和卸载。
最近的一个app应用分发的项目中app下载的模块,下载完成之后,用户可以通过这个app进行安装,为了提高用户的体验,我就加入了静默安装和卸载功能,然后还加入了使用am命令启动某个Activity。
这个项目中静默的方式实现代码如下:
首先判断是否有root权限,如果有利用静默方式,否则利用意图实现app安装和卸载操作。
- 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;
- /**
- * 描述: app安装操作
- * @author 吴传龙
- * Email:[email protected]
- * QQ: 3026862225
- * @version 创建时间: 2015年3月6日 下午3:51:14
- * @version 最后修改时间:2015年3月6日 下午3:51:14 修改人:吴传龙
- */
- public class ApkController {
- /**
- * 描述: 安装
- * 修改人: 吴传龙
- * 最后修改时间:2015年3月8日 下午9:07:50
- */
- 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;
- }
- }
- /**
- * 描述: 卸载
- * 修改人: 吴传龙
- * 最后修改时间:2015年3月8日 下午9:07:50
- */
- 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;
- }
- }
- }
要用其他的方式实现静默方式,可以通过伪装成系统应用,这就要给app打上系统应用的签名,但是这些签名在小米等手机上是没用的,所以这里不做介绍。还有就是通过把应用放在system/app的目录下也可以实现。
- 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;
- /**
- * 描述: MainActivity
- * @author 吴传龙
- * Email:[email protected]
- * QQ: 3026862225
- * @version 创建时间: 2015年3月9日 上午8:19:19
- * @version 最后修改时间:2015年3月9日 上午8:19:19 修改人:吴传龙
- */
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- /**
- * 描述: 安装
- * @param
- * 修改人: 吴传龙
- * 最后修改时间:2015年3月9日 上午8:19:30
- */
- 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();
- }
- /**
- * 描述: 卸载
- * @param
- * 修改人: 吴传龙
- * 最后修改时间:2015年3月9日 上午8:19:30
- */
- public void click2(View view){
- new Thread(){
- public void run() {
- if (ApkController.uninstall("com.example.jnitest", getApplicationContext())){
- toast("卸載成功");
- }else{
- toast("卸載失败");
- }
- };
- }.start();
- }
- /**
- * 描述: 启动
- * @param
- * 修改人: 吴传龙
- * 最后修改时间:2015年3月9日 上午8:19:30
- */
- 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() {
- @Override
- public void run() {
- Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();;
- }
- });
- }
- }
来源: http://www.phpxs.com/code/1002309/