美文网首页
Android部分手机通知权限关闭无法打出Toast

Android部分手机通知权限关闭无法打出Toast

作者: 鲁班0号 | 来源:发表于2019-02-15 17:51 被阅读0次

    0.现象描述

    比如华为,三星,魅族等一些手机,如果关闭某个应用的通知权限,然后应用类的toast就无法打出来了,表示很无解,难道要自己做一个view给用户提示吗?答案是当然不需要,还可以拯救一下。。。

    1.导致无法弹出toast的原因

    为了查一下原因,我们先去看看源码,下图是Toast的show方法

        /**
         * 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
            }
        }
    

    Toast中的show()方法,show方法中调用 service.enqueueToast(pkg, tn, mDuration); 那么service是什么呢?下面看看getService()的方法。

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

    INotificationManager 是跨进程通信的 Binder 类,sService 是 NMS(NotificationManagerService) 在客户端的代理,发送通知要委托给 sService,由它传递给 NMS,这里获取了远程的一个服务,用来调用远程的enqueueToast方法。
    接下来看看NotificationManagerService.java中的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 ;
                }
                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);
                    }
                }
            }
    

    从以上代码可知,如果noteNotificationOp()方法返回了false,也就是这个包名的通知权限被禁用,那么就会return,致使无法弹出Toast!

    2.怎么解决

    参考网络上的一些解决方案, 如下方案是把pkg名字改成“android”,这样就不用管权限了。

    /**
         * 显示系统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();
            }
        }

    相关文章

      网友评论

          本文标题:Android部分手机通知权限关闭无法打出Toast

          本文链接:https://www.haomeiwen.com/subject/ehcpeqtx.html