android u盘两种方式使用(广播和挂载)
来源:互联网 发布:mac如何剪切复制 编辑:程序博客网 时间:2024/06/06 01:57
该资料通过网上资料总结:
1.通过检测
String path = "/storage/uhost2";boolean mounted = isMounted(path);
private static final String MOUNTS_FILE = "/proc/mounts";public static boolean isMounted(String path) { boolean blnRet = false; String strLine = null; BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(MOUNTS_FILE)); while ((strLine = reader.readLine()) != null) { if (strLine.contains(path)) { blnRet = true; break; } } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } reader = null; } } return blnRet;}
2.通过广播方式检测
广播方面
package com.wuyue.horld;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.text.TextUtils;import android.util.Log;import android.widget.Toast;import java.io.File;public class USBReceiver extends BroadcastReceiver { private static final String TAG = USBReceiver.class.getSimpleName(); private BRInteraction brInteraction; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) { String mountPath = intent.getData().getPath(); Log.d(TAG, "mountPath = " + mountPath); if (!TextUtils.isEmpty(mountPath)) { //读取到U盘路径再做其他业务逻辑 //发送广播 File file = new File(mountPath + "/" + "111.zip"); if (file.exists()) { file.delete(); } File file1 = new File(mountPath + "/" + "FINGER_MARKS/"); if (file1.exists()) { file1.delete(); } try { ZipUtil.zipFolder("data/data/" + context.getPackageName() + "/" + "FINGER_MARKS/", mountPath + "/" + "111.zip"); Toast.makeText(context, "压缩成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } } } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED) || action.equals(Intent.ACTION_MEDIA_EJECT)) { Toast.makeText(context, "No services information detected !", Toast.LENGTH_SHORT).show(); } else if (action.equals("android.intent.action.BOOT_COMPLETED")) { //如果是开机完成,则需要调用另外的方法获取U盘的路径 Toast.makeText(context, "请重新插入u盘", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "请重新插入u盘", Toast.LENGTH_SHORT).show(); } } public interface BRInteraction { public void setText(String content); } public void setBRInteractionListener(BRInteraction brInteraction) { this.brInteraction = brInteraction; }}
界面
btzip.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //方式1 通过广播的形式进行zip到指定u盘 intentFilter = new IntentFilter(); intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED); intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED); intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); receiver = new USBReceiver(); registerReceiver(receiver, intentFilter); }});
广播清单文件
<receiver android:name="com.wuyue.horld.USBReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.MEDIA_REMOVED" /> <action android:name="android.intent.action.MEDIA_MOUNTED" /> <action android:name="android.intent.action.MEDIA_REMOVED" /> <action android:name="android.intent.action.MEDIA_REMOVED" /> <data android:scheme="file"></data> </intent-filter></receiver>
3 linux 得到u盘信息
https://github.com/alt236/USB-Device-Info---Android
阅读全文
1 0
- android u盘两种方式使用(广播和挂载)
- Android 广播的使用方式
- Android进阶之路 - BroadcaseReceiver(自定义广播、有序广播、无序广播、广播拦截、动态注册、静态注册)的详细使用方式
- android中广播的使用(动态和静态注册)
- Android 广播注册方式
- Android学习和使用广播BroadCastReceiver
- Android广播的两种注册使用方式。
- Android入门:广播发送者与广播接收者(同步广播和有序广播)
- Android广播之注册广播(包括静态广播和动态广播的注册)源码分析
- Android广播和广播接收者
- Android 的广播机制和两种注册方式
- Android定时广播和定时服务两种实现方式
- Android Broadcast广播注册和发布方式以及机制流程
- Android定时广播和定时服务两种实现方式
- android广播和短信广播详解----使用广播接收者窃听短信
- android广播和短信广播详解----使用广播接收者监听短信
- android广播和短信广播详解----使用广播接收者窃听短信
- Android BroadCast 广播的两种注册方式和粘性广播实现
- canvas动画时钟
- PyObject_CallObject 不运行,返回NULL
- ubuntu常用操作
- 朱熹《劝学文》
- ios 超出父控件区域支持点击事件
- android u盘两种方式使用(广播和挂载)
- 1.springboot注解详解
- LeetCode题库——LongestSubstringWithoutRepeatingCharacters
- 成为一名推荐系统工程师永远都不晚
- SAP中创建分部机构凭证号码并且按年度编号
- 【电子老鼠闯迷宫!】_(°:з」∠)_真让人头秃!!!!!
- 基于SnapDragonBoard410c的远程防盗系统
- Android----仿京东购物车(二级列表,mvp请求数据)
- Agri-Net