99import android .content .pm .Signature ;
1010import android .graphics .drawable .Drawable ;
1111
12+ import java .io .BufferedReader ;
1213import java .io .File ;
14+ import java .io .IOException ;
15+ import java .io .InputStreamReader ;
16+ import java .io .OutputStream ;
1317import java .util .ArrayList ;
1418import java .util .List ;
1519
@@ -55,7 +59,7 @@ public static void installApp(Context context, String filePath) {
5559 * @param file 文件
5660 */
5761 public static void installApp (Context context , File file ) {
58- if (file == null ) return ;
62+ if (! FileUtils . isFileExists ( file ) ) return ;
5963 context .startActivity (IntentUtils .getInstallAppIntent (file ));
6064 }
6165
@@ -78,10 +82,26 @@ public static void installApp(Activity activity, String filePath, int requestCod
7882 * @param requestCode 请求值
7983 */
8084 public static void installApp (Activity activity , File file , int requestCode ) {
81- if (file == null ) return ;
85+ if (! FileUtils . isFileExists ( file ) ) return ;
8286 activity .startActivityForResult (IntentUtils .getInstallAppIntent (file ), requestCode );
8387 }
8488
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+
85105 /**
86106 * 卸载App
87107 *
@@ -105,6 +125,22 @@ public static void uninstallApp(Activity activity, String packageName, int reque
105125 activity .startActivityForResult (IntentUtils .getUninstallAppIntent (packageName ), requestCode );
106126 }
107127
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+
108144 /**
109145 * 打开App
110146 *
@@ -385,7 +421,8 @@ public static boolean isAppForeground(Context context, String packageName) {
385421 ActivityManager am = (ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE );
386422 @ SuppressWarnings ("deprecation" )
387423 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 );
389426 }
390427
391428 /**
0 commit comments