Toast 在通知欄關閉時不顯示

toast 在應用關閉通知顯示時,某些手機上會不在顯示toast;

原因簡單的說就是toast使用了通知管理器INotificationManager類,而此類因爲禁止了通知欄權限而不顯示toast; 有興趣的可以追下源碼;

重點說下3種解決方法把:

  • 提示用戶打開通知欄權限
		var CHECK_OP_NO_THROW: String = "checkOpNoThrow"
        var OP_POST_NOTIFICATION: String = "OP_POST_NOTIFICATION"
        fun isNotificationEnabled(): Boolean {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                return (Utils.getContext().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).areNotificationsEnabled()
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                var mAppOps = Utils.getContext().getSystemService(Context.APP_OPS_SERVICE)
                var appInfo: ApplicationInfo = Utils.getContext().getApplicationInfo()
                var pkg: String = Utils.getContext().getApplicationContext().getPackageName()
                var uid: Int = appInfo.uid

                var appOpsClass: Class<*>? = null
                /* Context.APP_OPS_MANAGER */
                try {
                    appOpsClass = Class.forName(AppOpsManager::class.java.name)

                    var checkOpNoThrowMethod: Method = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String::class.java)
                    var opPostNotificationValue: Field = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION)

                    var value: Int = opPostNotificationValue.get(Integer::class.java) as Int
                    return (checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) as Int == AppOpsManager.MODE_ALLOWED)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
            return true
        }
  • 完全自定義toast或者彈框,規避通知管理器限制

自定義toast解決通知欄關閉

  • INotificationManager 根據包名判斷提示權限,使用動態代理使用android替換包名達到允許彈框的目的;

showSystemToast

	/**
     * 顯示系統Toast
     */
    private static void showSystemToast(Toast toast){
        try{
            Method getServiceMethod = Toast.class.getDeclaredMethod("getService");
            getServiceMethod.setAccessible(true);

            final Object iNotificationManager = getServiceMethod.invoke(null);
            Class iNotificationManagerCls = Class.forName("android.app.INotificationManager");
            Object iNotificationManagerProxy = Proxy.newProxyInstance(toast.getClass().getClassLoader(), new Class[]{iNotificationManagerCls}, new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    // 強制使用系統Toast
                    // 華爲p20 pro上爲enqueueToastEx
                    if("enqueueToast".equals(method.getName())
                            || "enqueueToastEx".equals(method.getName())){
                        args[0] = "android";
                    }
                    return method.invoke(iNotificationManager, args);
                }
            });
            Field sServiceFiled = Toast.class.getDeclaredField("sService");
            sServiceFiled.setAccessible(true);
            sServiceFiled.set(null, iNotificationManagerProxy);
            toast.show();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

個人偏向第三種動態代理的方案,具體使用判斷是否開啓通知欄權限使用第一種或者第三種方案;

記錄下toast爲什麼打不開的原因吧, read the fuck source code ~

--------------------------------toast 的 show方法----------------------------
最終獲取 `NotificationManagerService` service;

 /**
     * Show the view for the specified duration.
     */
    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
        }
    }

 static private INotificationManager getService() {
        if (sService != null) {
            return sService;
        }
        sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification"));
        return sService;
    }

---------------------NotificationManagerService 的 enqueueToast 方法----------------------
public void enqueueToast(String pkg, ITransientNotification callback, int duration)
        {
            if (DBG) {
                Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
            }
            if (pkg == null || callback == null) {
                Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
                return ;
            }
            //通過動態代理更改了包名爲android後,isSystemToasta爲true,可規避下面的reture;
            final boolean isSystemToast = isCallerSystem() || ("android".equals(pkg));
            final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, Binder.getCallingUid());
			
			//此行會根據包名判斷通知欄的權限是否禁用;
            if (ENABLE_BLOCKED_TOASTS && (!noteNotificationOp(pkg, Binder.getCallingUid())
                    || isPackageSuspended)) {

                if (!isSystemToast) {
                    Slog.e(TAG, "Suppressing toast from package " + pkg
                            + (isPackageSuspended? " due to package suspended by administrator."  : " by user request."));
                    return;
                }
            }
            synchronized (mToastQueue) {
                int callingPid = Binder.getCallingPid();
                long callingId = Binder.clearCallingIdentity();
                try {
                    ToastRecord record;
                    int index = indexOfToastLocked(pkg, callback);
                    // If it's already in the queue, we update it in place, we don't
                    // move it to the end of the queue.
                    if (index >= 0) {
                        record = mToastQueue.get(index);
                        record.update(duration);
                    } else {
                        // Limit the number of toasts that any given package except the android
                        // package can enqueue.  Prevents DOS attacks and deals with leaks.
                        if (!isSystemToast) {
                            int count = 0;
                            final int N = mToastQueue.size();
                            for (int i=0; i<N; i++) {
                                final ToastRecord r = mToastQueue.get(i);
                                if (r.pkg.equals(pkg)) {
                                    count++;
                                    if (count >= MAX_PACKAGE_NOTIFICATIONS) {
                                        Slog.e(TAG, "Package has already posted " + count
                                                + " toasts. Not showing more. Package=" + pkg);
                                        return;
                                    }
                                }
                            }
                        }
                        record = new ToastRecord(callingPid, pkg, callback, duration);
                        mToastQueue.add(record);
                        index = mToastQueue.size() - 1;
                        keepProcessAliveLocked(callingPid);
                    }

                    // If it's at index 0, it's the current toast.  It doesn't matter if it's
                    // new or just been updated.  Call back and tell it to show itself.
                    // If the callback fails, this will remove it from the list, so don't
                    // assume that it's valid after this.

                    if (index == 0) {
                        showNextToastLocked();
                    }
                } finally {
                    Binder.restoreCallingIdentity(callingId);
                }
            }
        }

解決方法很巧妙,實測可以用,學習了;

發佈了46 篇原創文章 · 獲贊 3 · 訪問量 5098
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章