Android通知栏不准弹东西

本文探讨了在Android系统中禁用通知的方法,深入解析了NotificationManager.java中的notify函数实现,并详细介绍了MtpNotificationManager类如何管理USB连接时的MTP存储设备通知,包括显示和隐藏通知的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、也就是禁用了Notification

NotificationManager.java

找到notify的根源,不执行

    /**
     * @hide
     */
    public void notifyAsUser(String tag, int id, Notification notification, UserHandle user)
    {
        //wfy
		if(isBST()){
			return;
		}
		int[] idOut = new int[1];
        INotificationManager service = getService();
        String pkg = mContext.getPackageName();
        // Fix the notification as best we can.
        Notification.addFieldsFromContext(mContext, notification);
        if (notification.sound != null) {
            notification.sound = notification.sound.getCanonicalUri();
            if (StrictMode.vmFileUriExposureEnabled()) {
                notification.sound.checkFileUriExposed("Notification.sound");
            }
        }
        fixLegacySmallIcon(notification, pkg);
        if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
            if (notification.getSmallIcon() == null) {
                throw new IllegalArgumentException("Invalid notification (no valid small icon): "
                        + notification);
            }
        }
        if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
        final Notification copy = Builder.maybeCloneStrippedForDelivery(notification);
        try {
            service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
                    copy, idOut, user.getIdentifier());
            if (localLOGV && id != idOut[0]) {
                Log.v(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
            }
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

2、取出usb连接时的通知

/hdisk2/chengsj/rk3288_606_nougat/frameworks/base/services/usb/java/com/android/server/usb/MtpNotificationManager.java

/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions an
 * limitations under the License.
 */

package com.android.server.usb;
import android.os.SystemProperties;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.UserHandle;

/**
 * Manager for MTP storage notification.
 */
class MtpNotificationManager {
    private static final String TAG = "UsbMtpNotificationManager";

    /**
     * Subclass for PTP.
     */
    private static final int SUBCLASS_STILL_IMAGE_CAPTURE = 1;

    /**
     * Subclass for Android style MTP.
     */
    private static final int SUBCLASS_MTP = 0xff;

    /**
     * Protocol for Picture Transfer Protocol (PIMA 15470).
     */
    private static final int PROTOCOL_PTP = 1;

    /**
     * Protocol for Android style MTP.
     */
    private static final int PROTOCOL_MTP = 0;

    private static final String ACTION_OPEN_IN_APPS = "com.android.server.usb.ACTION_OPEN_IN_APPS";

    private final Context mContext;
    private final OnOpenInAppListener mListener;

    MtpNotificationManager(Context context, OnOpenInAppListener listener) {
        mContext = context;
        mListener = listener;
        final Receiver receiver = new Receiver();
        context.registerReceiver(receiver, new IntentFilter(ACTION_OPEN_IN_APPS));
    }
    private boolean isBST(){
	boolean b=false;
	b=SystemProperties.getBoolean("ro.fnf.BST", false);
	return b;
    }
	//wfy
    void showNotification(UsbDevice device) {
		if(isBST()){
			return;
		}
        final Resources resources = mContext.getResources();
        final String title = resources.getString(
                com.android.internal.R.string.usb_mtp_launch_notification_title,
                device.getProductName());
        final String description = resources.getString(
                com.android.internal.R.string.usb_mtp_launch_notification_description);
        final Notification.Builder builder = new Notification.Builder(mContext)
                .setContentTitle(title)
                .setContentText(description)
                .setSmallIcon(com.android.internal.R.drawable.stat_sys_data_usb)
                .setCategory(Notification.CATEGORY_SYSTEM);

        final Intent intent = new Intent(ACTION_OPEN_IN_APPS);
        intent.putExtra(UsbManager.EXTRA_DEVICE, device);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_FOREGROUND);

        final PendingIntent openIntent = PendingIntent.getBroadcastAsUser(
                mContext,
                device.getDeviceId(),
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT,
                UserHandle.SYSTEM);
        builder.setContentIntent(openIntent);

        final Notification notification = builder.build();
        notification.flags |= Notification.FLAG_LOCAL_ONLY;

        mContext.getSystemService(NotificationManager.class).notify(
                TAG, device.getDeviceId(), notification);
    }

    void hideNotification(int deviceId) {
        mContext.getSystemService(NotificationManager.class).cancel(TAG, deviceId);
    }

    private class Receiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            final UsbDevice device =
                    intent.getExtras().<UsbDevice>getParcelable(UsbManager.EXTRA_DEVICE);
            if (device == null) {
                return;
            }
            switch (intent.getAction()) {
                case ACTION_OPEN_IN_APPS:
                    mListener.onOpenInApp(device);
                    break;
            }
        }
    }

    static boolean shouldShowNotification(PackageManager packageManager, UsbDevice device) {
        // We don't show MTP notification for devices that has FEATURE_AUTOMOTIVE.
        return !packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE) &&
                isMtpDevice(device);
    }

    private static boolean isMtpDevice(UsbDevice device) {
        for (int i = 0; i < device.getInterfaceCount(); i++) {
            final UsbInterface usbInterface = device.getInterface(i);
            if ((usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_STILL_IMAGE &&
                    usbInterface.getInterfaceSubclass() == SUBCLASS_STILL_IMAGE_CAPTURE &&
                    usbInterface.getInterfaceProtocol() == PROTOCOL_PTP)) {
                return true;
            }
            if (usbInterface.getInterfaceClass() == UsbConstants.USB_SUBCLASS_VENDOR_SPEC &&
                    usbInterface.getInterfaceSubclass() == SUBCLASS_MTP &&
                    usbInterface.getInterfaceProtocol() == PROTOCOL_MTP &&
                    "MTP".equals(usbInterface.getName())) {
                return true;
            }
        }
        return false;
    }

    static interface OnOpenInAppListener {
        void onOpenInApp(UsbDevice device);
    }
}

 

转载于:https://my.oschina.net/wfy94/blog/3049834

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值