android 5.1 下 u盘插拔
/**
* 用于监听android 5.1 的u盘插拔
*/
public class USBdiskReceiver51 extends BroadcastReceiver {
private static final String TAG = "USBdiskReceiver51";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
String path = intent.getData().getPath();
Log.e(TAG, action);
if (action.equals(VolumeInfo.ACTION_USB_DEVICE_ATTACHED)) {
// Log.e("action===", "装载");
} else if (action.equals(VolumeInfo.ACTION_USB_DEVICE_DETACHED)) {
//Log.d("action===", "卸载USB");
} else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
//Log.d(MainActivity.TAG, "ACTION_MEDIA_UNMOUNTED");
// processUnmountedMessage(context, bundle);
} else if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
//Log.d(MainActivity.TAG, "ACTION_MEDIA_MOUNTED");
processMountedMessage(context, path);
} else if (action.equals(Intent.ACTION_MEDIA_REMOVED)) {
//Log.d(MainActivity.TAG, "ACTION_MEDIA_REMOVED");
// processUnmountedMessage(context, bundle);
}
}
private void processUnmountedMessage(Context context, Bundle bundle) {
Intent mIntent = new Intent(MainActivity.ACTION_USB_RECEIVER);
mIntent.putExtra("message", "false");
context.sendBroadcast(mIntent);
}
private void processMountedMessage(Context context, String path) {
Log.e(TAG, "path ==>>" + path);
if (util.isPathExist(path)) {
Log.e(TAG, "into processMountedMessage isPathExist true");
Intent intent = new Intent(MainActivity.ACTION_USB_RECEIVER);
intent.putExtra("data", "USB_MOUNT");
intent.putExtra("path", path);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
//发送本地广播
localBroadcastManager.sendBroadcast(intent);
}
}
}
注册:
android 6.0 下 监听u盘插拔:
添加权限:
public class USBdiskReceiver extends BroadcastReceiver {
private static final String TAG = "USBdiskReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
Log.e("action===", action);
if (action.equals(VolumeInfo.ACTION_USB_DEVICE_ATTACHED)) {
// Log.e("action===", "装载");
} else if (action.equals(VolumeInfo.ACTION_USB_DEVICE_DETACHED)) {
//Log.d("action===", "卸载USB");
} else if (action.equals(VolumeInfo.ACTION_VOLUME_STATE_CHANGED)) {
int intExtra = intent.getIntExtra(VolumeInfo.EXTRA_VOLUME_STATE, VolumeInfo.STATE_UNMOUNTED);
Log.e("action===", "ACTION_VOLUME_STATE_CHANGED=intExtra=>>" + intExtra);
if (intExtra == VolumeInfo.STATE_MOUNTED) {
processMountedMessage(context);
}
}
}
private void processUnmountedMessage(Context context, Bundle bundle) {
Intent mIntent = new Intent(MainActivity.ACTION_USB_RECEIVER);
mIntent.putExtra("message", "false");
context.sendBroadcast(mIntent);
}
private void processMountedMessage(Context context) {
String path = util.getStoragePath(context);
if (util.isPathExist(path)) {
Log.e(TAG, "into processMountedMessage isPathExist true");
Intent intent = new Intent(MainActivity.ACTION_USB_RECEIVER);
intent.putExtra("data", "USB_MOUNT");
intent.putExtra("path", path);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
//发送本地广播
localBroadcastManager.sendBroadcast(intent);
}
}
}
VolumeInfo:
package com.example.petrochina_oil;
public class VolumeInfo {
/**
* U盘挂载类型 插入U盘 STATE_UNMOUNTED->STATE_MOUNTED;拔出U盘 STATE_EJECTING->STATE_UNMOUNTED->STATE_BAD_REMOVAL
*/
//卸载
public static final int STATE_UNMOUNTED = 0;
//状态监测
public static final int STATE_CHECKING = 1;
//挂载完成
public static final int STATE_MOUNTED = 2;
//只读
public static final int STATE_MOUNTED_READ_ONLY = 3;
//格式化
public static final int STATE_FORMATTING = 4;
//开始移除
public static final int STATE_EJECTING = 5;
//无法挂载
public static final int STATE_UNMOUNTABLE = 6;
//删除
public static final int STATE_REMOVED = 7;
//移除完成
public static final int STATE_BAD_REMOVAL = 8;
/**
* 接受广播key
*/
public static final String ACTION_VOLUME_STATE_CHANGED = "android.os.storage.action.VOLUME_STATE_CHANGED";
public static final String EXTRA_VOLUME_ID = "android.os.storage.extra.VOLUME_ID";
public static final String EXTRA_VOLUME_STATE = "android.os.storage.extra.VOLUME_STATE";
/**
* 刚挂载和关闭
*/
public static final String ACTION_USB_DEVICE_ATTACHED= "android.hardware.usb.action.USB_DEVICE_ATTACHED";
public static final String ACTION_USB_DEVICE_DETACHED= "android.hardware.usb.action.USB_DEVICE_DETACHED";
}
注册:
android 6.0 下u盘加载后获取路径:
public static String getStoragePath(Context context) {
String storagePath = null;
StorageManager mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
try {
Class storeManagerClazz = Class.forName("android.os.storage.StorageManager");
Method getVolumesMethod = storeManagerClazz.getMethod("getVolumes");
List> volumeInfos = (List>) getVolumesMethod.invoke(mStorageManager);//获取到了VolumeInfo的列表
Class volumeInfoClazz = Class.forName("android.os.storage.VolumeInfo");
Method getFsUuidMethod = volumeInfoClazz.getMethod("getFsUuid");
Field pathField = volumeInfoClazz.getDeclaredField("path");
if (volumeInfos != null) {
for (Object volumeInfo : volumeInfos) {
String uuid = (String) getFsUuidMethod.invoke(volumeInfo);
if (uuid != null) {
storagePath = (String) pathField.get(volumeInfo);
}
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return storagePath;
}
android 6.0 下正常情况会收不到 ACTION_VOLUME_STATE_CHANGED 消息,两个办法:
1、给应用加上系统key
2、修改源码 framework\base\services\core\java\com\android\server\pm\PackageManagerService.java
在 grantPermissionsLPw 方法的 case PermissionInfo.PROTECTION_SIGNATURE 中
if (allowedSig )
修改为
if (allowedSig || perm.equals(“android.permission.WRITE_MEDIA_STORAGE”) )
//下面的没试过,应该可行
这个if 为true 是关键,如果你想某个app可以申请全部权限
if (allowedSig || pkg.packageName.equals(你APP包名))
参考:
https://blog.csdn.net/subingxi/article/details/81541167