9
9
import android .content .pm .Signature ;
10
10
import android .graphics .drawable .Drawable ;
11
11
12
+ import java .io .BufferedReader ;
12
13
import java .io .File ;
14
+ import java .io .IOException ;
15
+ import java .io .InputStreamReader ;
16
+ import java .io .OutputStream ;
13
17
import java .util .ArrayList ;
14
18
import java .util .List ;
15
19
@@ -55,7 +59,7 @@ public static void installApp(Context context, String filePath) {
55
59
* @param file 文件
56
60
*/
57
61
public static void installApp (Context context , File file ) {
58
- if (file == null ) return ;
62
+ if (! FileUtils . isFileExists ( file ) ) return ;
59
63
context .startActivity (IntentUtils .getInstallAppIntent (file ));
60
64
}
61
65
@@ -78,10 +82,26 @@ public static void installApp(Activity activity, String filePath, int requestCod
78
82
* @param requestCode 请求值
79
83
*/
80
84
public static void installApp (Activity activity , File file , int requestCode ) {
81
- if (file == null ) return ;
85
+ if (! FileUtils . isFileExists ( file ) ) return ;
82
86
activity .startActivityForResult (IntentUtils .getInstallAppIntent (file ), requestCode );
83
87
}
84
88
89
+ /**
90
+ * 静默安装App
91
+ * <p>非root需添加权限 {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
92
+ *
93
+ * @param context 上下文
94
+ * @param filePath 文件路径
95
+ * @return {@code true}: 安装成功<br>{@code false}: 安装失败
96
+ */
97
+ public static boolean installAppSilent (Context context , String filePath ) {
98
+ File file = FileUtils .getFileByPath (filePath );
99
+ if (!FileUtils .isFileExists (file )) return false ;
100
+ String command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install " + filePath ;
101
+ ShellUtils .CommandResult commandResult = ShellUtils .execCmd (command , !isSystemApp (context ), true );
102
+ return commandResult .successMsg != null && commandResult .successMsg .toLowerCase ().contains ("success" );
103
+ }
104
+
85
105
/**
86
106
* 卸载App
87
107
*
@@ -105,6 +125,22 @@ public static void uninstallApp(Activity activity, String packageName, int reque
105
125
activity .startActivityForResult (IntentUtils .getUninstallAppIntent (packageName ), requestCode );
106
126
}
107
127
128
+ /**
129
+ * 静默卸载App
130
+ * <p>非root需添加权限 {@code <uses-permission android:name="android.permission.DELETE_PACKAGES" />}</p>
131
+ *
132
+ * @param context 上下文
133
+ * @param packageName 包名
134
+ * @param isKeepData 是否保留数据
135
+ * @return {@code true}: 卸载成功<br>{@code false}: 卸载成功
136
+ */
137
+ public static boolean uninstallAppSilent (Context context , String packageName , boolean isKeepData ) {
138
+ if (StringUtils .isSpace (packageName )) return false ;
139
+ String command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib pm uninstall " + (isKeepData ? "-k " : "" ) + packageName ;
140
+ ShellUtils .CommandResult commandResult = ShellUtils .execCmd (command , !isSystemApp (context ), true );
141
+ return commandResult .successMsg != null && commandResult .successMsg .toLowerCase ().contains ("success" );
142
+ }
143
+
108
144
/**
109
145
* 打开App
110
146
*
@@ -385,7 +421,8 @@ public static boolean isAppForeground(Context context, String packageName) {
385
421
ActivityManager am = (ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE );
386
422
@ SuppressWarnings ("deprecation" )
387
423
List <ActivityManager .RunningTaskInfo > tasks = am .getRunningTasks (1 );
388
- return !tasks .isEmpty () && tasks .get (0 ).topActivity .getPackageName ().equals (packageName );
424
+ return tasks != null && !tasks .isEmpty ()
425
+ && tasks .get (0 ).topActivity .getPackageName ().equals (packageName );
389
426
}
390
427
391
428
/**
0 commit comments