Android静默安装和静默卸载

来源:互联网 发布:软件测试难不难 编辑:程序博客网 时间:2024/05/01 04:53

转自:http://www.2cto.com/kf/201503/381170.html


静默顾名思义就是静静的默默地,静默安装和静默卸载的意思也就是说在后台默默地安装和卸载。

最近的一个app应用分发的项目中app下载的模块,下载完成之后,用户可以通过这个app进行安装,为了提高用户的体验,我就加入了静默安装和卸载功能,然后还加入了使用am命令启动某个Activity。

这个项目中静默的方式实现代码如下:

首先判断是否有root权限,如果有利用静默方式,否则利用意图实现app安装和卸载操作。


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
packagecom.example.test;
 
importjava.io.File;
importjava.io.IOException;
importjava.io.PrintWriter;
 
importandroid.content.Context;
importandroid.content.Intent;
importandroid.net.Uri;
 
/**
 * 描述: app安装操作             
 * @author 吴传龙                 
 * Email:andywuchuanlong@sina.cn 
 * QQ: 3026862225              
 * @version 创建时间: 2015年3月6日 下午3:51:14               
 * @version 最后修改时间:2015年3月6日 下午3:51:14     修改人:吴传龙     
 */
publicclass ApkController {
    /**
     * 描述: 安装
     * 修改人: 吴传龙                                             
     * 最后修改时间:2015年3月8日 下午9:07:50
     */
    publicstatic boolean install(String apkPath,Context context){
        // 先判断手机是否有root权限
        if(hasRootPerssion()){
            // 有root权限,利用静默安装实现
            returnclientInstall(apkPath);
        }else{
            // 没有root权限,利用意图进行安装
            File file = newFile(apkPath);
            if(!file.exists())
                returnfalse;
            Intent intent = newIntent();
            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);
            returntrue;
        }
    }
     
    /**
     * 描述: 卸载
     * 修改人: 吴传龙                                             
     * 最后修改时间:2015年3月8日 下午9:07:50
     */
    publicstatic boolean uninstall(String packageName,Context context){
        if(hasRootPerssion()){
            // 有root权限,利用静默卸载实现
            returnclientUninstall(packageName);
        }else{
            Uri packageURI = Uri.parse("package:"+ packageName);
            Intent uninstallIntent = newIntent(Intent.ACTION_DELETE,packageURI);
            uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(uninstallIntent);
            returntrue;
        }
    }
     
    /**
     * 判断手机是否有root权限
     */
    privatestatic boolean hasRootPerssion(){
        PrintWriter PrintWriter = null;
        Process process = null;
        try{
            process = Runtime.getRuntime().exec("su");
            PrintWriter = newPrintWriter(process.getOutputStream());
            PrintWriter.flush();
            PrintWriter.close();
            intvalue = process.waitFor(); 
            returnreturnResult(value);
        }catch(Exception e) {
            e.printStackTrace();
        }finally{
            if(process!=null){
                process.destroy();
            }
        }
        returnfalse;
    }
     
    /**
     * 静默安装
     */
    privatestatic boolean clientInstall(String apkPath){
        PrintWriter PrintWriter = null;
        Process process = null;
        try{
            process = Runtime.getRuntime().exec("su");
            PrintWriter = newPrintWriter(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();
            intvalue = process.waitFor(); 
            returnreturnResult(value);
        }catch(Exception e) {
            e.printStackTrace();
        }finally{
            if(process!=null){
                process.destroy();
            }
        }
        returnfalse;
    }
     
    /**
     * 静默卸载
     */
    privatestatic boolean clientUninstall(String packageName){
        PrintWriter PrintWriter = null;
        Process process = null;
        try{
            process = Runtime.getRuntime().exec("su");
            PrintWriter = newPrintWriter(process.getOutputStream());
            PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib ");
            PrintWriter.println("pm uninstall "+packageName);
            PrintWriter.flush();
            PrintWriter.close();
            intvalue = process.waitFor(); 
            returnreturnResult(value);
        }catch(Exception e) {
            e.printStackTrace();
        }finally{
            if(process!=null){
                process.destroy();
            }
        }
        returnfalse;
    }
     
    /**
     * 启动app
     * com.exmaple.client/.MainActivity
     * com.exmaple.client/com.exmaple.client.MainActivity
     */
    publicstatic boolean startApp(String packageName,String activityName){
        booleanisSuccess = false;
        String cmd = "am start -n " + packageName + "/"+ activityName + " \n";
        Process process = null;
        try{
           process = Runtime.getRuntime().exec(cmd);
           intvalue = process.waitFor(); 
           returnreturnResult(value);
        }catch(Exception e) {
          e.printStackTrace();
        }finally{
            if(process!=null){
                process.destroy();
            }
        }
        returnisSuccess;
    }
     
     
    privatestatic boolean returnResult(intvalue){
        // 代表成功 
        if(value == 0) {
            returntrue;
        }elseif (value == 1) { // 失败
            returnfalse;
        }else{ // 未知情况
            returnfalse;
        
    }
}


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
packagecom.example.test;
 
importjava.io.File;
 
importandroid.support.v4.app.Fragment;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.view.LayoutInflater;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.Toast;
importandroid.os.Build;
/**
 * 描述: MainActivity            
 * @author 吴传龙                 
 * Email:andywuchuanlong@sina.cn 
 * QQ: 3026862225              
 * @version 创建时间: 2015年3月9日 上午8:19:19               
 * @version 最后修改时间:2015年3月9日 上午8:19:19     修改人:吴传龙
 */
publicclass MainActivity extendsActivity {
 
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    /**
     * 描述: 安装
     * @param          
     * 修改人: 吴传龙                                             
     * 最后修改时间:2015年3月9日 上午8:19:30
     */
    publicvoid click1(View view){
        newThread(){
            publicvoid run() {
                String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/jniTest.apk";
                if(ApkController.install(path, getApplicationContext())){
                    toast("安裝成功");
                }else{
                    toast("安裝失败");
                }
            };
        }.start();
    }
     
    /**
     * 描述: 卸载
     * @param          
     * 修改人: 吴传龙                                             
     * 最后修改时间:2015年3月9日 上午8:19:30
     */
    publicvoid click2(View view){
        newThread(){
            publicvoid run() {
                if(ApkController.uninstall("com.example.jnitest", getApplicationContext())){
                    toast("卸載成功");
                }else{
                    toast("卸載失败");
                }
            };
        }.start();
    }
     
    /**
     * 描述: 启动
     * @param          
     * 修改人: 吴传龙                                             
     * 最后修改时间:2015年3月9日 上午8:19:30
     */
    publicvoid click3(View view){
        if(ApkController.startApp("com.example.jnitest","com.example.jnitest.MainActivity")) {
            toast("啟動成功");
        }
    }
     
     
    publicvoid toast(finalString text){
        runOnUiThread(newRunnable() {
            @Override
            publicvoid run() {
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();;
            }
        });
    }
 
}

要用其他的方式实现静默方式,可以通过伪装成系统应用,这就要给app打上系统应用的签名,但是这些签名在小米等手机上是没用的,所以这里不做介绍。还有就是通过把应用放在system/app的目录下也可以实现。
0 0