Skip to content

Commit b2e47b7

Browse files
committed
see 10/08 log
1 parent 8a0302d commit b2e47b7

File tree

3 files changed

+348
-15
lines changed

3 files changed

+348
-15
lines changed

utilcode/src/main/java/com/blankj/utilcode/utils/AppUtils.java

+40-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
import android.content.pm.Signature;
1010
import android.graphics.drawable.Drawable;
1111

12+
import java.io.BufferedReader;
1213
import java.io.File;
14+
import java.io.IOException;
15+
import java.io.InputStreamReader;
16+
import java.io.OutputStream;
1317
import java.util.ArrayList;
1418
import 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
/**

utilcode/src/main/java/com/blankj/utilcode/utils/IntentUtils.java

+21-10
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ public static Intent getComponentIntent(String packageName, String className, Bu
167167
return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
168168
}
169169

170+
/**
171+
* 获取关机的意图
172+
* <p>需添加权限 {@code <uses-permission android:name="android.permission.SHUTDOWN"/>}</p>
173+
*
174+
* @return intent
175+
*/
176+
public static Intent getShutdownIntnet() {
177+
Intent intent = new Intent(Intent.ACTION_SHUTDOWN);
178+
return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
179+
}
180+
170181
/**
171182
* 获取拍照的意图
172183
*
@@ -178,25 +189,25 @@ public static Intent getCaptureIntent(Uri outUri) {
178189
intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);
179190
return intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
180191
}
181-
182-
/**
192+
/*
193+
*//**
183194
* 获取选择照片的Intent
184195
*
185196
* @return
186-
*/
197+
*//*
187198
public static Intent getPickIntentWithGallery() {
188199
Intent intent = new Intent(Intent.ACTION_PICK);
189-
return intent.setType("image/*");
200+
return intent.setType("image*//*");
190201
}
191202
192-
/**
203+
*//**
193204
* 获取从文件中选择照片的Intent
194205
*
195206
* @return
196-
*/
207+
*//*
197208
public static Intent getPickIntentWithDocuments() {
198209
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
199-
return intent.setType("image/*");
210+
return intent.setType("image*//*");
200211
}
201212
202213
@@ -213,7 +224,7 @@ public static Intent buildImageGetIntent(Uri saveTo, int aspectX, int aspectY,
213224
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
214225
intent.addCategory(Intent.CATEGORY_OPENABLE);
215226
}
216-
intent.setType("image/*");
227+
intent.setType("image*//*");
217228
intent.putExtra("output", saveTo);
218229
intent.putExtra("aspectX", aspectX);
219230
intent.putExtra("aspectY", aspectY);
@@ -232,7 +243,7 @@ public static Intent buildImageCropIntent(Uri uriFrom, Uri uriTo, int outputX, i
232243
public static Intent buildImageCropIntent(Uri uriFrom, Uri uriTo, int aspectX, int aspectY,
233244
int outputX, int outputY, boolean returnData) {
234245
Intent intent = new Intent("com.android.camera.action.CROP");
235-
intent.setDataAndType(uriFrom, "image/*");
246+
intent.setDataAndType(uriFrom, "image*//*");
236247
intent.putExtra("crop", "true");
237248
intent.putExtra("output", uriTo);
238249
intent.putExtra("aspectX", aspectX);
@@ -249,5 +260,5 @@ public static Intent buildImageCaptureIntent(Uri uri) {
249260
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
250261
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
251262
return intent;
252-
}
263+
}*/
253264
}

0 commit comments

Comments
 (0)