安卓USB挂载,卸载广播接收类
来源:互联网 发布:同学通讯录查询软件 编辑:程序博客网 时间:2024/06/18 13:07
(一)说明:
接上USB,接收广播;拔掉USB,接收广播。并且可以获得USB的目录打印。方便拓展功能。
(二)步骤:
1.注册
<receiver android:name="com.harison.receive.USBBroadcastReceiver" > <intent-filter android:priority="999" > <action android:name="android.intent.action.MEDIA_MOUNTED" > </action> <action android:name="android.intent.action.MEDIA_EJECT" > </action> <action android:name="android.intent.action.MEDIA_UNMOUNTED" > </action> <data android:scheme="file" > </data> </intent-filter> </receiver>
2.类代码
package com.harison.receive;import java.io.File;import java.util.HashMap;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.os.StatFs;import android.util.Log;public class USBBroadcastReceiver extends BroadcastReceiver { private static int count = 0; private static HashMap<String, Integer> diskDesc = new HashMap<String, Integer>(); private static final String TAG = "USBBroadcastReceiver"; public static synchronized void addDiskDesc(String key, Integer value) { diskDesc.put(key, value); } public static synchronized void removeDiskDesc(String key) { diskDesc.remove(key); } public static String formatKey(String value) { if (value == null) { return null; } if (!value.startsWith("/")) { value = "/" + value; } if (value.endsWith("/")) { value = value.substring(0, value.length() - 1); return formatKey(value); } else { return value; } } /** * @param source * @return */ public static String splitHeadAndTail(String source) { if (source == null) { return null; } if (source.startsWith("/")) { source = source.substring(1); } if (source.endsWith("/")) { source = source.substring(0, source.length() - 1); } return source; } private static synchronized void initDiskDesc() { String parent = "/mnt/usb/"; File pFile = new File(parent); File[] children = pFile.listFiles(); if (children != null) { String str = null; for (File file : children) { str = file.getName(); if (str != null) { Log.e(TAG, "find disk " + str); addDiskDesc(parent + str, 0); } } } } public USBBroadcastReceiver() { Log.d(TAG, "AD ----- USBBroadcastReceiver constructor!!!"); if (count == 0) { initDiskDesc(); count++; } } /** * @return disk count */ public static synchronized int getDiskCount() { if (diskDesc != null) { return diskDesc.size(); } else { return 0; } } /** * return disk capacity percent by path * * @param key * @return */ public static synchronized int getDiskCapacityPercent(String key) { Integer tmp = diskDesc.get(key); if (tmp == null) { return -1; } else { return tmp; } } public static synchronized boolean isDiskExisted(String path) { path = formatKey(path); if (diskDesc.containsKey(path)) { return true; } else { return false; } } @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_MEDIA_MOUNTED) || action.equals(Intent.ACTION_MEDIA_EJECT)) { Uri uri = intent.getData(); final String path = uri.getPath(); Log.w(TAG, "path:" + path); if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) { Log.w(TAG, "AD ---- ACTION_MEDIA_MOUNTED:" + path); addDiskDesc(path, 0); new Thread() { public void run() { try { StatFs sf = new StatFs(path); final int percent = (int) (100 - ((sf.getFreeBlocks() * 100)/ sf.getBlockCount())); } catch (IllegalArgumentException e) { Log.e(TAG, "Error : ", e); } }; }.start(); } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) { Log.w(TAG, "AD --- ACTION_MEDIA_UNMOUNTED:" + path); } else if (action.equals(Intent.ACTION_MEDIA_EJECT)) { Log.w(TAG, "AD ---- ACTION_MEDIA_EJECT:" + path); removeDiskDesc(path); } } }}
(三)实验现象
06-12 13:59:06.593: D/USBBroadcastReceiver(2581): USBBroadcastReceiver constructor!!!06-12 13:59:06.593: E/USBBroadcastReceiver(2581): find disk sda1<span style="color:#ff6666;">-------------- 发现usb</span>06-12 13:59:06.783: W/USBBroadcastReceiver(2581): AD ---- ACTION_MEDIA_MOUNTED:/mnt/usb/sda106-12 13:59:26.168: D/USBBroadcastReceiver(2776): USBBroadcastReceiver constructor!!!06-12 13:59:26.168: W/USBBroadcastReceiver(2776): path:/mnt/usb/sda106-12 13:59:26.168: W/USBBroadcastReceiver(2776): ACTION_MEDIA_EJECT:/mnt/usb/sda1-<span style="color:#ff0000;">--------------usb挂载成功</span>06-12 13:59:26.188: D/USBBroadcastReceiver(2581): USBBroadcastReceiver constructor!!!06-12 13:59:26.188: W/USBBroadcastReceiver(2581): path:/mnt/usb/sda106-12 13:59:26.188: W/USBBroadcastReceiver(2581): AD ---- ACTION_MEDIA_EJECT:/mnt/usb/sda1 <span style="color:#ff0000;">------------ usb卸载成功</span>
0 0
- 安卓USB挂载,卸载广播接收类
- 安卓usb数据接收
- 安卓短信拦截通过广播接收
- 安卓UDP接收广播数据
- 安卓 广播有时候会接收不到
- 安卓广播监听USB插拔
- 安卓获得屏幕状态(非广播被动接收)
- 安卓开发静态订阅广播接收失败
- 安卓监听手机USB接口拔插警报广播
- 接收卸载程序后的广播
- 应用卸载和安装广播接收
- 安卓intent发起广播事件给系统或当前app,并从系统或当前app中接收广播
- 安卓设备作为广播的接收端和发送端
- 安卓BroadcastReceiver组件使用系列1:普通广播的发送和接收
- Android开发初级00_1如何在安卓开发之接收广播
- Android 接收安装, 卸载,更新等系统广播
- 安卓发送广播
- 【安卓】Broadcast广播
- u-boot启动内核的几种方式--bootm
- datagridview显示行号
- Application中数据传递及内存泄漏问题
- Drupal批量删除大数量用户的办法,一次性想删多少删多少。
- github优秀开源项目大全-iOS
- 安卓USB挂载,卸载广播接收类
- android开发:[1]如何安装apk应用到虚拟机
- jQuery向动态生成的内容添加事件响应(jquery live方法简介)
- 高效的项目和团队
- 回头看看走过的路,想想下一步该怎么走
- Android 4.3 系统裁剪——删除不使用的app及增加自己app
- HBase常用操作之namespace
- GRE作文考试——ARGUMENT类范文
- 苹果 Cocoa 编程规范 中文版