吐司通知未在某些手机中显示

时间:2018-10-10 16:34:09

标签: android android-toast

我正在开发一个应用程序,其中包括显示有关各种信息的吐司通知。在此手机 Nokia 6.1(Android 8)上进行测试时,我意识到没有显示任何吐司。

我正在按照Android官方开发人员指南中所述的正常方式使用Toast通知。

我再次检查了我的应用是否具有通知权限。

然后,为了测试电话端是否有任何异常,我决定查看如何在此移动设备中实施Gmail中“邮件已发送”的通知。通常,它看起来像烤面包,但在这部手机上,它看起来像是小吃吧。

我想这是Android的自定义实现。假设这是自定义实现,那么如何在我的应用程序中容纳此类异常?我应该切换到小吃店还是有其他替代方法来解决这个问题?

我个人希望吐司显示信息,而切换到小吃店会导致应用程序发生很多潜在的变化。在支持大多数Android手机的同时,我能做些什么来最大程度地减少影响?

我正在使用以下方法显示烤面包:

public static void showLongToast(Context context, String message) {
        Toast t = Toast.makeText(context, message, Toast.LENGTH_LONG);
        t.show();
}

我调试了t.show(),然后指针进入了Toast.java中的这段代码。

 public void show() {
        if (mNextView == null) {
            throw new RuntimeException("setView must have been called");
        }

        INotificationManager service = getService();
        String pkg = mContext.getOpPackageName();
        TN tn = mTN;
        tn.mNextView = mNextView;

        try {
            service.enqueueToast(pkg, tn, mDuration);
        } catch (RemoteException e) {
            // Empty
        }
    }

指针进入throw new RuntimeException("setView must have been called");。另外,try块没有执行。

1 个答案:

答案 0 :(得分:0)

我认为诺基亚实施Toasts的方式可能存在问题。

在AOSP中,makeText()就是这样做的(公共方法只是使用空Looper调用此方法):

public static Toast makeText(@NonNull Context context, @Nullable Looper looper,
        @NonNull CharSequence text, @Duration int duration) {
    Toast result = new Toast(context, looper);

    LayoutInflater inflate = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
    TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
    tv.setText(text);

    result.mNextView = v;
    result.mDuration = duration;

    return result;
}

注意如何将mNextView设置为内部视图。

我的理论是诺基亚以某种方式破坏了这种方法(也许他们删除了该布局,或者根本没有设置View)。我正在下载诺基亚6.1库存ROM,以查看是否可以找到它,如果发现任何内容,我将对其进行更新。

与此同时,这是一个可能的解决方法:

public static void showLongToast(Context context, String message) {
    Toast t = Toast.makeText(context, message, Toast.LENGTH_LONG);
    if (t.getView() == null) {
        int layoutRes = context.getResources().getIdentifier("transient_notification", "layout", "android");
        int tvRes = context.getResources().getIdentifier("message", "id", "android");
        View layout = LayoutInflater.from(context).inflate(layoutRes, null);
        TextView textView = layout.findViewById(tvRes);

        textView.setText(message);
        t.setView(layout);
    }
    t.show();
}
相关问题