android如何使用u盘监听的库文件

本文详细介绍了在Android环境中如何监听USB存储设备的插入和移除事件,包括在AndroidManifest.xml中添加必要的receiver和intent-filter,以及针对Android 6.0及以上的系统权限设置。此外,还提供了实现本地广播接收器的代码示例,用于实时获取USB设备的状态变化。

库文件下载地址:https://download.youkuaiyun.com/download/cau_eric/11818730 

2019.9.25
如何使用jar文件
1、在AndroidManifest.xml中添加两个receiver

<receiver android:name="com.example.usbmonitor.USBdiskReceiver">
    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
        <action android:name="android.os.storage.action.VOLUME_STATE_CHANGED" />
    </intent-filter>
</receiver>
<receiver android:name="com.example.usbmonitor.USBdiskReceiver51">
    <intent-filter android:priority="1000">
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
        <action android:name="android.intent.action.MEDIA_REMOVED" />
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />

        <data android:scheme="file" />
    </intent-filter>
</receiver>

2、对于android 6.0 还需添加权限

<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />


这是系统权限,需要有系统签名,一个给应用添加系统签名,一个是直接修改系统源码,把权限放开
具体参考:https://blog.youkuaiyun.com/cau_eric/article/details/100932028


3、在调用的activity中声明本地广播,可以获取到u盘插拔的消息和u盘加载的路径
//==============声明变量=====================>> 

private LocalBroadcastManager localBroadcastManager;
private IntentFilter intentFilter;
private LocalReceiver localReceiver;
//===============注册/反注册====================>>
@Override
protected void onResume() {
    super.onResume();

    localBroadcastManager = LocalBroadcastManager.getInstance(this);
    intentFilter = new IntentFilter();
    intentFilter.addAction(Constant.ACTION_USB_RECEIVER);
    localReceiver = new LocalReceiver();
    //注册本地接收器
    localBroadcastManager.registerReceiver(localReceiver, intentFilter);
}

@Override
protected void onPause() {
    super.onPause();

    localBroadcastManager.unregisterReceiver(localReceiver);
}
//===================实现====================================>>

private class LocalReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String data = intent.getStringExtra("data");
        Message msg = Message.obtain();
        Log.d(TAG, "收到本地广播==>>" + data);
        if (data.equals("USB_MOUNT")) {
            //msg.what = MyHandler.USB_MOUNT;
            msg.obj = intent.getStringExtra("path");
            Log.d(TAG, "收到本地广播=path=>>" +  msg.obj );
        }
        //mHandler.sendMessage(msg);
    }
}
//===============完======================>>>

库文件下载地址:https://download.youkuaiyun.com/download/cau_eric/11818730 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值