吐司有时无法显示在屏幕上

时间:2020-05-22 06:22:45

标签: android

为了避免重复烤面包,我写了#define

ToastUtils

public class ToastUtils { private ToastUtils() { //no instance } private static Toast toast = null; public static void showToast(String message) { if (toast == null) { toast = Toast.makeText(Utils.getApp(), message, Toast.LENGTH_SHORT); } toast.setText(message); toast.setDuration(Toast.LENGTH_SHORT); toast.show(); } public static void showToast(@StringRes int messageId) { if (toast == null) { toast = Toast.makeText(Utils.getApp(), messageId, Toast.LENGTH_SHORT); } toast.setText(messageId); toast.setDuration(Toast.LENGTH_SHORT); toast.show(); } } 如下:

Utils.java

public class Utils { private Utils() { //no instance } /** * 提供统一的 Context 接口给其他工具类 * * @return Context 对象 */ public static Context getApp() { return App.getApplication(); } } 是我的自定义App.java

Application

让我感到沮丧的是public class App extends Application { private static App application; @Override public void onCreate() { super.onCreate(); application = this; String curProcessName = getCurProcessName(); if (!TextUtils.equals(curProcessName, getPackageName())) { return; } // omit my business code... } public static App getApplication() { return application; } private String getCurProcessName() { int pid = android.os.Process.myPid(); ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); if (am == null || am.getRunningAppProcesses() == null || am.getRunningAppProcesses().isEmpty()) { return null; } for (ActivityManager.RunningAppProcessInfo appProcess : am.getRunningAppProcesses()) { if (appProcess.pid == pid) { return appProcess.processName; } } return null; } } 有时不起作用,即我看不到屏幕上显示的吐司消息。

2 个答案:

答案 0 :(得分:0)

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: svc2
          servicePort: 80
        path: /cacheable-path

第一个参数应填充活动

答案 1 :(得分:0)

如何将上下文放入参数中?

public static void showToast(Context context, @StringRes int messageId) {
    if (toast == null) {
        toast = Toast.makeText(context, messageId, Toast.LENGTH_SHORT);
    }
    toast.setText(messageId);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.show();
}

活动中:

showToast(this, R.string.toast)