Android:如何检测“打开USB存储”广播?
问题描述:
我想检测打开USB存储使用广播接收器虽然我能够检测到USB使用连接android.intent.action.UMS_CONNECTED行动
和
断开使用android.intent.action。 UMS_DISCONNECTED操作。如何检测USB存储器?Android:如何检测“打开USB存储”广播?
答
以下是我如何检查存储卡是否安装/卸下。你可以改变它来检查删除/ insterted。我这样做是通过注册一个BroadcastReceiver来获取“mount事件”,然后检查存储卡处于何种状态。如果没有安装它,并且不是在检查时(重新安装卡时的状态),它将被卸载或该卡已被移除。
public class MemCardReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
onMemcardMounted();
}
else if (!Environment.getExternalStorageState().equals(Environment.MEDIA_CHECKING)){
onMemorycardUnMounted();
}
}
private void onMemorycardUnMounted() {}
private void onMemcardMounted() {}
}
而且在manifest资源配置文件
<receiver android:enabled="true" android:exported="true" android:name="the.name">
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<action android:name="android.intent.action.MEDIA_UNMOUNTED" />
<data android:scheme="file" />
</intent-filter>
</receiver>
有几种不同的状态checkout this如果有任何其他规定等。删除
答
我认为android.Intent.action.ACTION_MEDIA_EJECT广播当用户使用USB存储和ACTION_MEDIA_MOUNTED关闭时。