Skip to content

Commit 065688b

Browse files
authored
Merge pull request #77 from weifengzz/MIUtils_branch
修改MIUIUtils类中isMIUI方法android 8.0以上异常
2 parents fd22cc7 + 38bf446 commit 065688b

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

android/src/main/java/org/wonday/aliyun/push/AliyunPushModule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void getDeviceId(final Promise promise) {
9191
@ReactMethod
9292
public void setApplicationIconBadgeNumber(int badgeNumber, final Promise promise) {
9393

94-
if (MIUIUtils.isMIUI()) { //小米特殊处理
94+
if (MIUIUtils.isMIUI(getReactApplicationContext())) { //小米特殊处理
9595
FLog.d(ReactConstants.TAG, "setApplicationIconBadgeNumber for xiaomi");
9696

9797
if (badgeNumber==0) {
@@ -260,7 +260,7 @@ public void onHostResume() {
260260
public void onHostPause() {
261261

262262
//小米特殊处理, 处于后台时更新角标, 否则会被系统清除,看不到
263-
if (MIUIUtils.isMIUI()) {
263+
if (MIUIUtils.isMIUI(getReactApplicationContext())) {
264264
FLog.d(ReactConstants.TAG, "onHostPause:setBadgeNumber for xiaomi");
265265
MIUIUtils.setBadgeNumber(this.context, getCurrentActivity().getClass(), badgeNumber);
266266
}
@@ -271,7 +271,7 @@ public void onHostPause() {
271271
public void onHostDestroy() {
272272

273273
//小米特殊处理, 处于后台时更新角标, 否则会被系统清除,看不到
274-
if (MIUIUtils.isMIUI()) {
274+
if (MIUIUtils.isMIUI(getReactApplicationContext())) {
275275
FLog.d(ReactConstants.TAG, "onHostDestroy:setBadgeNumber for xiaomi");
276276
MIUIUtils.setBadgeNumber(this.context, getCurrentActivity().getClass(), badgeNumber);
277277
}

android/src/main/java/org/wonday/aliyun/push/MIUIUtils.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,23 @@ public class MIUIUtils {
4747
private static int notificationId = (int)(System.currentTimeMillis()/1000);
4848

4949

50-
public static boolean isMIUI() {
50+
public static boolean isMIUI(Context context) {
5151
if(hasChecked)
5252
{
5353
return isMIUI;
5454
}
55-
56-
Properties prop= new Properties();
57-
5855
try {
59-
prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
60-
} catch (IOException e)
56+
SystemProperty sp = new SystemProperty(context);
57+
if (sp.getOrThrow(KEY_MIUI_VERSION_CODE) != null || sp.getOrThrow(KEY_MIUI_VERSION_NAME) != null || sp.getOrThrow(KEY_MIUI_INTERNAL_STORAGE) != null) {
58+
hasChecked = true;
59+
isMIUI = true;
60+
}
61+
} catch (Exception e)
6162
{
6263
e.printStackTrace();
6364
return false;
6465
}
65-
66-
isMIUI= prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
67-
|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
68-
|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
69-
70-
hasChecked = true;
71-
66+
7267
return isMIUI;
7368
}
7469

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.wonday.aliyun.push;
2+
3+
import android.content.Context;
4+
5+
import java.lang.reflect.InvocationTargetException;
6+
import java.lang.reflect.Method;
7+
8+
public class SystemProperty {
9+
private final Context mContext;
10+
11+
public SystemProperty(Context mContext) {
12+
this.mContext = mContext;
13+
}
14+
15+
public String getOrThrow(String key) throws Exception {
16+
try {
17+
ClassLoader classLoader = mContext.getClassLoader();
18+
Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
19+
Method methodGet = SystemProperties.getMethod("get", String.class);
20+
return (String) methodGet.invoke(SystemProperties, key);
21+
} catch (Exception e) {
22+
throw new Exception(e);
23+
}
24+
}
25+
26+
public String get(String key) {
27+
try {
28+
return getOrThrow(key);
29+
} catch (Exception e) {
30+
return null;
31+
}
32+
}
33+
34+
}

0 commit comments

Comments
 (0)