Android学习 ——SystemUIService之StorageNotification分析
来源:互联网 发布:淘宝商城女童鞋 编辑:程序博客网 时间:2024/06/03 21:24
usb 这块的内容较多,但是抽取核心部分之后就比较容易理解
**查看文件目录,其实在SystemUIService 启动中相关的类只有StorageNotification.java和UsbStorageActivity.java
StorageNotification负责逻辑处理,UsbStorageActivity负责显示**
下面是StorageNotification.java 的主要函数关系图
先看入口函数
@Override public void start() { mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE); final boolean connected = mStorageManager.isUsbMassStorageConnected(); if (DEBUG) Log.d(TAG, String.format( "Startup with UMS connection %s (media state %s)", mUmsAvailable, Environment.getExternalStorageState())); HandlerThread thr = new HandlerThread("SystemUI StorageNotification"); thr.start(); mAsyncEventHandler = new Handler(thr.getLooper()); StorageNotificationEventListener listener = new StorageNotificationEventListener(); listener.onUsbMassStorageConnectionChanged(connected); mStorageManager.registerListener(listener); }
先获取了STORAGE_SERVICE,然后给mStorageManager 注册了监听 StorageNotificationEventListener
private class StorageNotificationEventListener extends StorageEventListener { //连接改变 public void onUsbMassStorageConnectionChanged(final boolean connected) { mAsyncEventHandler.post(new Runnable() { @Override public void run() { onUsbMassStorageConnectionChangedAsync(connected); } }); } // 存储状态改变 public void onStorageStateChanged(final String path, final String oldState, final String newState) { mAsyncEventHandler.post(new Runnable() { @Override public void run() { onStorageStateChangedAsync(path, oldState, newState); } }); } }
StorageNotificationEventListener 主要是监听两个函数
onUsbMassStorageConnectionChanged(usb连接变化)
onStorageStateChanged (存储状态变化)
在这两个函数中又主要使用了
onUsbMassStorageConnectionChangedAsync()
onStorageStateChangedAsync()
在事件监听中会返回一系列的状态值,根据不同的状态值配置显示标题,内容,使用UsbStorageActivity 显示
代码片段截取 if (newState.equals(Environment.MEDIA_CHECKING)) { /* * Storage is now checking. Update media notification and disable * UMS notification. */ setMediaStorageNotification( com.android.internal.R.string.ext_media_checking_notification_title, com.android.internal.R.string.ext_media_checking_notification_message, com.android.internal.R.drawable.stat_notify_sdcard_prepare, true, false, null); updateUsbMassStorageNotification(false); }
上述配置需要显示的内容
下面为配置到UI显示以及设置发送存储相关的通知
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); } }
private synchronized void setUsbStorageNotification(int titleId, int messageId, int icon, boolean sound, boolean visible, PendingIntent pi) { // force to show UsbSettings screen to select usb mode if property is true if (SystemProperties.getBoolean("persist.sys.ums", true)) { titleId = 0; messageId = 0; icon = 0; sound = false; visible = false; pi = null; } if (!visible && mUsbStorageNotification == null) { return; } NotificationManager notificationManager = (NotificationManager) mContext .getSystemService(Context.NOTIFICATION_SERVICE); if (notificationManager == null) { return; } if (visible) { 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.DEFAULT_SOUND; } else { mUsbStorageNotification.defaults &= ~Notification.DEFAULT_SOUND; } mUsbStorageNotification.flags = Notification.FLAG_ONGOING_EVENT; mUsbStorageNotification.tickerText = title; if (pi == null) { Intent intent = new Intent(); pi = PendingIntent.getBroadcastAsUser(mContext, 0, intent, 0, UserHandle.CURRENT); } mUsbStorageNotification.setLatestEventInfo(mContext, title, message, pi); final boolean adbOn = 1 == Settings.Global.getInt( mContext.getContentResolver(), Settings.Global.ADB_ENABLED, 0); if (POP_UMS_ACTIVITY_ON_CONNECT && !adbOn) { // Pop up a full-screen alert to coach the user through enabling UMS. The average // user has attached the device to USB either to charge the phone (in which case // this is harmless) or transfer files, and in the latter case this alert saves // several steps (as well as subtly indicates that you shouldn't mix UMS with other // activities on the device). // // If ADB is enabled, however, we suppress this dialog (under the assumption that a // developer (a) knows how to enable UMS, and (b) is probably using USB to install // builds or use adb commands. mUsbStorageNotification.fullScreenIntent = pi; } } final int notificationId = mUsbStorageNotification.icon; if (visible) { notificationManager.notifyAsUser(null, notificationId, mUsbStorageNotification, UserHandle.ALL); } else { notificationManager.cancelAsUser(null, notificationId, UserHandle.ALL); } }
阅读全文
0 0
- Android学习 ——SystemUIService之StorageNotification分析
- Android学习——systemUIService 之PowerUI分析
- Android学习 ——SystemUIService之RingtonePlayer分析
- Android学习 ——SystemUIService之SettingsUI分析
- Android学习——systemUIService 类分析
- android 6.0 SystemUI源码分析(6)-StorageNotification介绍
- SystemUI源码分析一(SystemUIService的启动)
- android安全学习之6—signapk分析
- systemui开发---PowerUI && StorageNotification
- 【android学习之十二】——SDK实例NotePad 分析1
- 【android学习之十三】——SDK实例NotePad 分析2
- Android学习笔记之实现一个文档查看器(一)——应用分析
- 李兴华—JAVA学习之我见 分析!!!
- android 学习之---listview adapter分析
- Android Service学习之IntentService 深入分析
- Android Service学习之IntentService 深入分析
- Android Service学习之IntentService 深入分析
- Android Service学习之IntentService 深入分析
- PHP 使用CURL库IP欺骗,隐藏真实客户端IP
- Eclipse svn提交代码的一般步骤(对比、解决冲突、更新、提交)
- ViewPager学习
- MongoDB第一课,shell命令下的增删改查
- SSH整合
- Android学习 ——SystemUIService之StorageNotification分析
- 整数求和
- Node.js环境下对MongoDB实现查,插,分页功能
- 习题6.7
- 配置Canvas为全屏时,无滚动条
- android string.xml里空格表示
- 常用的转义字符表
- M
- android okhttp3框架使用