From 6d99bd5853a079b3494c9a7b3f4561807568c279 Mon Sep 17 00:00:00 2001 From: Blankj <625783482@qq.com> Date: Sun, 13 Nov 2016 12:01:27 +0800 Subject: [PATCH] see 11/12 log --- README-CN.md | 3 +++ .../activities/ActivityActivity.java | 8 +++---- .../activities/DeviceActivity.java | 6 +++++ app/src/main/res/layout/activity_device.xml | 16 +++++++++++++ app/src/main/res/values/string.xml | 2 ++ update_log.md | 1 + .../blankj/utilcode/utils/ActivityUtils.java | 23 +++++++++++++++++++ .../blankj/utilcode/utils/DeviceUtils.java | 16 +++++++++++++ .../blankj/utilcode/utils/EncryptUtils.java | 6 ++--- 9 files changed, 73 insertions(+), 8 deletions(-) diff --git a/README-CN.md b/README-CN.md index 015c0bd3c3..d16e2b88a1 100644 --- a/README-CN.md +++ b/README-CN.md @@ -6,6 +6,7 @@ ``` isActivityExists : 判断是否存在Activity launchActivity : 打开Activity +getLauncherActivity: 获取launcher activity ``` > - **App相关→[AppUtils.java][app.java]** @@ -117,6 +118,8 @@ getManufacturer : 获取设备厂商 getModel : 获取设备型号 shutdown : 关机 reboot : 重启 +reboot2Recovery : 重启到recovery +reboot2Bootloader : 重启到bootloader ``` > - **判空相关→[EmptyUtils.java][empty.java]→[Test][empty.test]** diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/ActivityActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/ActivityActivity.java index 2fad9e8d5a..cd5bf08fa5 100644 --- a/app/src/main/java/com/blankj/androidutilcode/activities/ActivityActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/activities/ActivityActivity.java @@ -7,9 +7,6 @@ import com.blankj.androidutilcode.R; import com.blankj.utilcode.utils.ActivityUtils; -import com.blankj.utilcode.utils.DeviceUtils; -import com.blankj.utilcode.utils.IntentUtils; -import com.blankj.utilcode.utils.ShellUtils; /** *
@@ -38,8 +35,9 @@ protected void onCreate(Bundle savedInstanceState) { findViewById(R.id.btn_launch_image_activity).setOnClickListener(this); - boolean isExists = ActivityUtils.isActivityExists(this, packageName, className); - tvAboutActivity.setText(String.format("Is ImageActivity Exists: %b", isExists)); + tvAboutActivity.setText("Is ImageActivity Exists: " + ActivityUtils.isActivityExists(this, packageName, className) + + "\ngetLauncherActivity: " + ActivityUtils.getLauncherActivity(this, packageName) + ); } @Override diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/DeviceActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/DeviceActivity.java index 5460e7df19..649d1a7660 100644 --- a/app/src/main/java/com/blankj/androidutilcode/activities/DeviceActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/activities/DeviceActivity.java @@ -29,6 +29,8 @@ protected void onCreate(Bundle savedInstanceState) { findViewById(R.id.btn_shutdown).setOnClickListener(this); findViewById(R.id.btn_reboot).setOnClickListener(this); + findViewById(R.id.btn_reboot_to_recovery).setOnClickListener(this); + findViewById(R.id.btn_reboot_to_bootloader).setOnClickListener(this); tvAboutDevice.setText("isRoot: " + DeviceUtils.isDeviceRoot() + "\ngetSDKVersion: " + DeviceUtils.getSDKVersion() + @@ -47,6 +49,10 @@ public void onClick(View view) { break; case R.id.btn_reboot: DeviceUtils.reboot(); + case R.id.btn_reboot_to_recovery: + DeviceUtils.reboot2Recovery(); + case R.id.btn_reboot_to_bootloader: + DeviceUtils.reboot2Bootloader(); break; } } diff --git a/app/src/main/res/layout/activity_device.xml b/app/src/main/res/layout/activity_device.xml index 7b8290e819..75ddf08e27 100644 --- a/app/src/main/res/layout/activity_device.xml +++ b/app/src/main/res/layout/activity_device.xml @@ -28,6 +28,22 @@ android:text="@string/device.reboot" /> + + + +Shutdown Reboot +Reboot To Recovery +Reboot To Bootloader Send Msg After 3s diff --git a/update_log.md b/update_log.md index be291043b6..2a82f50faf 100644 --- a/update_log.md +++ b/update_log.md @@ -1,4 +1,5 @@ ### +#### 16/11/12 最近一直在博客搬家,所以更得有点少,新增重启到recovery和bootloader,新增获取launcher activity #### 16/11/04 修复README的缺少process的bug #### 16/11/03 SnackbarUtils中Snackbar持有弱引用来消除内存泄漏 #### 16/11/02 内存泄漏检测中 diff --git a/utilcode/src/main/java/com/blankj/utilcode/utils/ActivityUtils.java b/utilcode/src/main/java/com/blankj/utilcode/utils/ActivityUtils.java index 2c39e9c0f7..1a3178871f 100644 --- a/utilcode/src/main/java/com/blankj/utilcode/utils/ActivityUtils.java +++ b/utilcode/src/main/java/com/blankj/utilcode/utils/ActivityUtils.java @@ -3,7 +3,10 @@ import android.app.ActivityManager; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.os.Bundle; +import android.util.Log; import java.util.List; @@ -59,4 +62,24 @@ public static void launchActivity(Context context, String packageName, String cl public static void launchActivity(Context context, String packageName, String className, Bundle bundle) { context.startActivity(IntentUtils.getComponentIntent(packageName, className, bundle)); } + + /** + * 获取launcher activity + * + * @param context 上下文 + * @param packageName 包名 + * @return launcher activity + */ + public static String getLauncherActivity(Context context, String packageName) { + Intent intent = new Intent(Intent.ACTION_MAIN, null); + intent.addCategory(Intent.CATEGORY_LAUNCHER); + PackageManager pm = context.getPackageManager(); + Listinfos = pm.queryIntentActivities(intent, 0); + for (ResolveInfo info : infos) { + if (info.activityInfo.packageName.equals(packageName)) { + return info.activityInfo.name; + } + } + return "no " + packageName; + } } diff --git a/utilcode/src/main/java/com/blankj/utilcode/utils/DeviceUtils.java b/utilcode/src/main/java/com/blankj/utilcode/utils/DeviceUtils.java index 1ebd0c373d..23d58fc04e 100644 --- a/utilcode/src/main/java/com/blankj/utilcode/utils/DeviceUtils.java +++ b/utilcode/src/main/java/com/blankj/utilcode/utils/DeviceUtils.java @@ -243,4 +243,20 @@ public static void reboot(Context context, String reason) { e.printStackTrace(); } } + + /** + * 重启到recovery + * 需要root权限
+ */ + public static void reboot2Recovery() { + ShellUtils.execCmd("reboot recovery", true); + } + + /** + * 重启到bootloader + *需要root权限
+ */ + public static void reboot2Bootloader() { + ShellUtils.execCmd("reboot bootloader", true); + } } \ No newline at end of file diff --git a/utilcode/src/main/java/com/blankj/utilcode/utils/EncryptUtils.java b/utilcode/src/main/java/com/blankj/utilcode/utils/EncryptUtils.java index d63fd75694..12286d3255 100644 --- a/utilcode/src/main/java/com/blankj/utilcode/utils/EncryptUtils.java +++ b/utilcode/src/main/java/com/blankj/utilcode/utils/EncryptUtils.java @@ -124,7 +124,7 @@ public static byte[] encryptMD5(byte[] data) { * @return 文件的16进制密文 */ public static String encryptMD5File2String(String filePath) { - File file = StringUtils.isSpace(filePath) ? null : new File(filePath); + File file = StringUtils.isSpace(filePath) ? null : new File(filePath); return encryptMD5File2String(file); } @@ -135,7 +135,7 @@ public static String encryptMD5File2String(String filePath) { * @return 文件的MD5校验码 */ public static byte[] encryptMD5File(String filePath) { - File file = StringUtils.isSpace(filePath) ? null : new File(filePath); + File file = StringUtils.isSpace(filePath) ? null : new File(filePath); return encryptMD5File(file); } @@ -164,7 +164,7 @@ public static byte[] encryptMD5File(File file) { MessageDigest md = MessageDigest.getInstance("MD5"); digestInputStream = new DigestInputStream(fis, md); byte[] buffer = new byte[256 * 1024]; - while (digestInputStream.read(buffer) > 0); + while (digestInputStream.read(buffer) > 0) ; md = digestInputStream.getMessageDigest(); return md.digest(); } catch (NoSuchAlgorithmException | IOException e) {