Android SystemUI 添加USBOTG存储设备卸载按钮
来源:互联网 发布:什么营销软件好 编辑:程序博客网 时间:2024/06/05 06:07
需求是,当插入USB设备时,在下拉状态栏里出现一个图标按钮,点击后卸载USBotg存储,同时,卸载完成时,该按钮消失。
实现思路,当插入USB存储时,通知栏会弹出“正在准备USB存储设备是否有误”,通过这句话找到弹出的位置发送一个自定义广播,然后在SystemUI里监听该广播更新存储按钮的状态即可。
先看插入时通知栏提示的代码位置
frameworks/base/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java
if (newState.equals(Environment.MEDIA_CHECKING)) {140 /*141 * Storage is now checking. Update media notification and disable142 * UMS notification.143 */144 setMediaStorageNotification(145 com.android.internal.R.string.ext_media_checking_notification_title,146 com.android.internal.R.string.ext_media_checking_notification_message,147 com.android.internal.R.drawable.stat_notify_sdcard_prepare, true, false, null);148 updateUsbMassStorageNotification(false); 149 }
如以上代码所示if里的判断条件是MEDIA_CHECKING。接下去的一个if就是MEDIA_MOUNTED在这个状态中发送广播即可
if (newState.equals(Environment.MEDIA_MOUNTED)) { 150 Intent intent = new Intent(); 151 intent.setAction("com.socsi.USB"); //设置意图发送广播 152 intent.putExtra("usb","mounted"); //参数是挂载了153 mContext.sendBroadcast(intent,null); //发送154 /*155 * Storage is now mounted. Dismiss any media notifications,156 * and enable UMS notification if connected.157 */158 setMediaStorageNotification(0, 0, 0, false, false, null);159 updateUsbMassStorageNotification(mUmsAvailable);160 }同理在MEDIA_UNMOUNTED和MEDIA_BAD_REMOVAL里也添加发送卸载USB存储设备的广播。
if (newState.equals(Environment.MEDIA_UNMOUNTED)) {//正常卸载状态161 Intent intent = new Intent(); 162 intent.setAction("com.socsi.USB"); 163 intent.putExtra("usb","unmounted");164 mContext.sendBroadcast(intent,null); 165 ………………//省略无关代码
if (newState.equals(Environment.MEDIA_BAD_REMOVAL)) {//直接拔出状态244 /*245 * Storage has been removed unsafely. Show bad removal media notification,246 * and disable UMS notification regardless of connection state.247 */248 Intent intent = new Intent(); 249 intent.setAction("com.socsi.USB"); 250 intent.putExtra("usb","unmounted");251 mContext.sendBroadcast(intent,null); 252 setMediaStorageNotification(253 com.android.internal.R.string.ext_media_badremoval_notification_title,254 com.android.internal.R.string.ext_media_badremoval_notification_message,255 com.android.internal.R.drawable.stat_sys_warning,256 true, true, null);257 updateUsbMassStorageNotification(false);258 }
发送状态的代码也就完成了。现在看看添加的新按钮,关于如何添加新按钮博客已经非常之多了。可以参考
http://blog.csdn.net/lyjit/article/details/51579067
45 public class SdcardTile extends QSTile<QSTile.BooleanState> { 46 private final String TAG = "qs_sdcard"; 47 48 private StorageManager mStorageManager; 49 private IMountService mMountService; 52 private boolean mountState = false; 53 public SdcardTile(Host host) {
54 super(host); 55 IntentFilter filter = new IntentFilter(); //注册监听广播 56 filter.addAction("com.socsi.USB"); 57 mContext.registerReceiver(mReceiver, filter); 58 67 } 73 74 @Override 75 public void handleClick() {//处理按钮点击事件 76 if(mountState) { 77 Toast.makeText(mContext, "系统将卸载USB存储设备" ,Toast.LENGTH_SHORT).show();//弹出一个提示吐司 78 IMountService mountService = getMountService(); 79 try { 80 mountService.unmountVolume("/storage/usbotg", true, false);//卸载设备 81 } catch (RemoteException e) { 82 // Informative dialog to user that unmount failed. 83 Toast.makeText(mContext, "卸载失败" ,Toast.LENGTH_SHORT).show(); 84 } 85 } 86 } 87 88 private synchronized IMountService getMountService() { 89 if (mMountService == null) { 90 IBinder service = ServiceManager.getService("mount"); 91 if (service != null) { 92 mMountService = IMountService.Stub.asInterface(service); 93 } else { 94 Log.e("luomf", "Can't get mount service"); 95 } 96 } 97 return mMountService; 98 } 99 103 104 105 @Override106 protected void handleUpdateState(BooleanState state, Object arg) {107 Log.d(TAG,"handleUpdateState");108 state.visible = false;//按钮默认不显示109 state.label = mContext.getString(R.string.quick_settings_no_otg);//按钮图标下面的字符串110 state.icon = ResourceIcon.get(R.drawable.usb_attach);//按钮的图标111 if(mountState){112 state.visible = true;//如果挂载了则显示按钮113 }else{114 state.visible = false;//否则不显示按钮115 }116 }117 118 119 private BroadcastReceiver mReceiver = new BroadcastReceiver(){ 120 121 @Override 122 public void onReceive(Context context, Intent intent) {123 String mountstate = intent.getExtras().getString("usb"); 124 Log.d("luomf", "接收到:"+mountstate); 125 mountState = mountstate.equals("mounted") ? true:false;126 refreshState(true);//这句话会刷新handleUpdateState127 } 128 129 };以上就是关于弹出图标、消失图标与卸载的逻辑实现方法。
1 0
- Android SystemUI 添加USBOTG存储设备卸载按钮
- Android SystemUI在导航栏添加音量加减按钮
- Android SystemUI状态栏添加图标
- Android SystemUI状态栏添加图标
- android systemui
- Android: SystemUI
- Android SystemUI 信号栏后添加信号图标
- mdev自动加/卸载usb存储设备
- SystemUI添加耳机图标
- 往SystemUI添加进度条
- Android 添加按钮
- android添加菜单按钮
- Android Edittext 添加按钮
- Android 存储设备
- 添加android设备号
- android SystemUI浅析之SystemUI启动流程
- android systemUI--Notification 整理
- android ics SystemUI详解
- Dom节点操作
- 进程与线程区别
- JavaScript DOM编程艺术 读书笔记
- 怼技术|维护服务器
- 消除启动时的白屏/黑屏
- Android SystemUI 添加USBOTG存储设备卸载按钮
- APP版本控制问题
- 获取网络时间、获取特定时区时间、时间同步GMT UTC
- sphinx管理shell
- mysql数据库基础汇总
- oracle安装-简介
- 欢迎使用CSDN-markdown编辑器
- iOS 一个方法首次安装滚播图 展示应用简介
- java的学习笔记