Skip to content

Commit adf5571

Browse files
committed
see 04/17 log
1 parent f652fdf commit adf5571

File tree

5 files changed

+45
-31
lines changed

5 files changed

+45
-31
lines changed

utilcode/README-CN.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,8 @@ getZodiac : 获取星座
636636
setGravity : 设置吐司位置
637637
setBgColor : 设置背景颜色
638638
setBgResource : 设置背景资源
639-
setMessageColor: 设置消息颜色
639+
setMsgColor : 设置消息颜色
640+
setMsgTextSize : 设置消息字体大小
640641
showShort : 显示短时吐司
641642
showLong : 显示长时吐司
642643
showCustomShort: 显示短时自定义吐司

utilcode/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,8 @@ getZodiac
636636
setGravity
637637
setBgColor
638638
setBgResource
639-
setMessageColor
639+
setMsgColor
640+
setMsgTextSize
640641
showShort
641642
showLong
642643
showCustomShort

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import android.annotation.SuppressLint;
44
import android.app.Activity;
5-
import android.app.ActivityManager;
6-
import android.content.Context;
75
import android.content.Intent;
86
import android.content.pm.ApplicationInfo;
97
import android.content.pm.PackageInfo;
@@ -332,17 +330,7 @@ public static boolean isAppSystem(final String packageName) {
332330
* @return {@code true}: yes<br>{@code false}: no
333331
*/
334332
public static boolean isAppForeground() {
335-
ActivityManager am =
336-
(ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
337-
if (am == null) return false;
338-
List<ActivityManager.RunningAppProcessInfo> info = am.getRunningAppProcesses();
339-
if (info == null || info.size() == 0) return false;
340-
for (ActivityManager.RunningAppProcessInfo aInfo : info) {
341-
if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
342-
return aInfo.processName.equals(Utils.getApp().getPackageName());
343-
}
344-
}
345-
return false;
333+
return Utils.isAppForeground();
346334
}
347335

348336
/**

utilcode/src/main/java/com/blankj/utilcode/util/ToastUtils.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@
3232
*/
3333
public final class ToastUtils {
3434

35-
private static final int COLOR_DEFAULT = 0xFEFFFFFF;
36-
private static final Handler HANDLER = new Handler(Looper.getMainLooper());
35+
private static final int COLOR_DEFAULT = 0xFEFFFFFF;
36+
private static final Handler HANDLER = new Handler(Looper.getMainLooper());
3737

38-
private static WeakReference<Toast> sToast;
39-
private static int sGravity = -1;
40-
private static int sXOffset = -1;
41-
private static int sYOffset = -1;
42-
private static int sBgColor = COLOR_DEFAULT;
43-
private static int sBgResource = -1;
44-
private static int sMsgColor = COLOR_DEFAULT;
38+
private static WeakReference<Toast> sWeakToast;
39+
private static int sGravity = -1;
40+
private static int sXOffset = -1;
41+
private static int sYOffset = -1;
42+
private static int sBgColor = COLOR_DEFAULT;
43+
private static int sBgResource = -1;
44+
private static int sMsgColor = COLOR_DEFAULT;
4545
private static int sMsgTextSize = -1;
4646

4747
private ToastUtils() {
@@ -191,6 +191,8 @@ public static void showLong(final String format, final Object... args) {
191191

192192
/**
193193
* Show custom toast for a short period of time.
194+
*
195+
* @param layoutId ID for an XML layout resource to load.
194196
*/
195197
public static View showCustomShort(@LayoutRes final int layoutId) {
196198
final View view = getView(layoutId);
@@ -200,6 +202,8 @@ public static View showCustomShort(@LayoutRes final int layoutId) {
200202

201203
/**
202204
* Show custom toast for a long period of time.
205+
*
206+
* @param layoutId ID for an XML layout resource to load.
203207
*/
204208
public static View showCustomLong(@LayoutRes final int layoutId) {
205209
final View view = getView(layoutId);
@@ -212,9 +216,9 @@ public static View showCustomLong(@LayoutRes final int layoutId) {
212216
*/
213217
public static void cancel() {
214218
Toast toast;
215-
if (sToast != null && (toast = sToast.get()) != null) {
219+
if (sWeakToast != null && (toast = sWeakToast.get()) != null) {
216220
toast.cancel();
217-
sToast = null;
221+
sWeakToast = null;
218222
}
219223
}
220224

@@ -236,7 +240,7 @@ private static void show(final CharSequence text, final int duration) {
236240
public void run() {
237241
cancel();
238242
Toast toast = Toast.makeText(Utils.getTopActivityOrApp(), text, duration);
239-
sToast = new WeakReference<>(toast);
243+
sWeakToast = new WeakReference<>(toast);
240244
final TextView tvMessage = toast.getView().findViewById(android.R.id.message);
241245
int msgColor = tvMessage.getCurrentTextColor();
242246
//it solve the font of toast
@@ -264,7 +268,7 @@ private static void show(final View view, final int duration) {
264268
public void run() {
265269
cancel();
266270
Toast toast = new Toast(Utils.getTopActivityOrApp());
267-
sToast = new WeakReference<>(toast);
271+
sWeakToast = new WeakReference<>(toast);
268272

269273
toast.setView(view);
270274
toast.setDuration(duration);

utilcode/src/main/java/com/blankj/utilcode/util/Utils.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.annotation.SuppressLint;
44
import android.app.Activity;
5+
import android.app.ActivityManager;
56
import android.app.Application;
67
import android.app.Application.ActivityLifecycleCallbacks;
78
import android.content.Context;
@@ -11,6 +12,7 @@
1112
import java.lang.reflect.Field;
1213
import java.lang.reflect.InvocationTargetException;
1314
import java.util.LinkedList;
15+
import java.util.List;
1416
import java.util.Map;
1517

1618
/**
@@ -111,7 +113,7 @@ public static Application getApp() {
111113
throw new NullPointerException("u should init first");
112114
}
113115

114-
static void setTopActivity(final Activity activity) {
116+
private static void setTopActivity(final Activity activity) {
115117
if (activity.getClass() == PermissionUtils.PermissionActivity.class) return;
116118
if (ACTIVITY_LIST.contains(activity)) {
117119
if (!ACTIVITY_LIST.getLast().equals(activity)) {
@@ -128,8 +130,26 @@ static LinkedList<Activity> getActivityList() {
128130
}
129131

130132
static Context getTopActivityOrApp() {
131-
Activity topActivity = getTopActivity();
132-
return topActivity == null ? Utils.getApp() : topActivity;
133+
if (isAppForeground()) {
134+
Activity topActivity = getTopActivity();
135+
return topActivity == null ? Utils.getApp() : topActivity;
136+
} else {
137+
return Utils.getApp();
138+
}
139+
}
140+
141+
static boolean isAppForeground() {
142+
ActivityManager am =
143+
(ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
144+
if (am == null) return false;
145+
List<ActivityManager.RunningAppProcessInfo> info = am.getRunningAppProcesses();
146+
if (info == null || info.size() == 0) return false;
147+
for (ActivityManager.RunningAppProcessInfo aInfo : info) {
148+
if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
149+
return aInfo.processName.equals(Utils.getApp().getPackageName());
150+
}
151+
}
152+
return false;
133153
}
134154

135155
static Activity getTopActivity() {

0 commit comments

Comments
 (0)