android root权限下静默 安装\卸载

首先判断是否有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
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 ;
        
     }
}


?
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
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 {
 
     @Override
     protected 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();
     }
     
     /**
      * 描述: 启动
      * @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();;
             }
         });
     }
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值