android 4.0.3 usb插拔提示音播放问题分析

前言:
最近客户看见别的android4.0.3机器插拔usb有播放提示音,而我们的机器没有。客户就开始抱怨了。
公司没有做应用的人,没办法,让我这个对java半桶水的人搞,哎,只有硬着头皮弄。
刚接到手,根本不知道从哪里开始,也不知道需要设置什么属性(后来看到源码里有读属性才知道),悲剧的很。
按理说,这种通知提示google应该是早就形成机制做好的,只要配置好,应该就可以了,不过事情并不是这么简单。...


无奈,logcat一下插拔usb时候的log,发现这个关键TAG:StorageNotification,就从这里开始。

1. StorageNotification.java
frameworks/base/packages/SystemUI/src/com/android/systemui/usb/
其实主要是看到了这一句log:
Slog.i(TAG, String.format("UMS connection changed to %s (media state %s)", connected, st));
所以就从这里开始分析:
private void onUsbMassStorageConnectionChangedAsync(boolean connected) {
        mUmsAvailable = connected;
        /*
         * Even though we may have a UMS host connected, we the SD card
         * may not be in a state for export.
         */
        String st = Environment.getExternalStorageState();


        Slog.i(TAG, String.format("UMS connection changed to %s (media state %s)", connected, st));


        if (connected && (st.equals(
                Environment.MEDIA_REMOVED) || st.equals(Environment.MEDIA_CHECKING))) {
            /*
             * No card or card being checked = don't display
             */
            connected = false;
        }
        updateUsbMassStorageNotification(connected);
    }
    参数connected表示usb是否已插入。
    /* private Notification mUsbStorageNotification;
       关于Notification和NotificationManager使用参考网址文章:
       http://yuanyao.iteye.com/blog/472332
  http://blog.csdn.net/feng88724/article/details/6259071
  http://www.cnblogs.com/stay/articles/1898963.html
    */
    void updateUsbMassStorageNotification(boolean available) {


        if (available) {
            Intent intent = new Intent();
            intent.setClass(mContext, com.android.systemui.usb.UsbStorageActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


            PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
            setUsbStorageNotification(
                    com.android.internal.R.string.usb_storage_notification_title,
                    com.android.internal.R.string.usb_storage_notification_message,
                    com.android.internal.R.drawable.stat_sys_data_usb,
                    false, true, pi);
        } else {
            setUsbStorageNotification(0, 0, 0, false, false, null);
        }
    }
    通过函数setUsbStorageNotification()的原型可以看出:
    private synchronized void setUsbStorageNotification(int titleId, int messageId, int icon,
            boolean sound, boolean visible, PendingIntent pi);
    第四个参数表示是否播放声音,看到这里我万分激动将这个false改成true,不过郁闷的时,插拔usb没有声音出来。
    再来看setUsbStorageNotification()函数的实现,基本上都是按照上面几篇文章介绍的那样来使用,第四个参数为true就设置使用默认提示音。
    private synchronized void setUsbStorageNotification(int titleId, int messageId, int icon,
            boolean sound, boolean visible, PendingIntent pi) {


        if (!visible && mUsbStorageNotification == null) {
            return;
        }// 通知是否正常的逻辑判断


        NotificationManager notificationManager = (NotificationManager) mContext
                .getSystemService(Context.NOTIFICATION_SERVICE);// 获取notificationManager实例,用来和notificationManagerService通信。


        if (notificationManager == null) {
            return;
        }
        
        if (visible) { // 从上面传下来的参数和log看,只有在插入usb的时候这个visible才为true,拔出为false。
            Resources r = Resources.getSystem();
            CharSequence title = r.getText(titleId);// 获取显示在状态栏上的标题
            CharSequence message = r.getText(messageId);// 获取显示在标题下的说明文字


            if (mUsbStorageNotification == null) {
                mUsbStorageNotification = new Notification();// 创建一个通知的实例
                mUsbStorageNotification.icon = icon; // 获取显示在状态栏上的图标
                mUsbStorageNotification.when = 0;
            }


            if (sound) { // 这里就是声音的设置了,要么使用默认,要么自己指定。
                mUsbStorageNotification.defaults |= Notification.D
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值