当设备作为大容量存储连接时,如何检测和处理我的应用程序?
我用SD资源开发一个游戏。当我将设备连接为大容量存储时,设备崩溃。这是因为当我将设备作为大容量存储插入 PC 时,该设备无法访问 SD 卡。所以我尝试了不同的方法来解决。
目前,我正在实现一个 BroadcastReceiver,并且正在处理操作 ACTION_MEDIA_BAD_REMOVAL (如果手动删除 SD 卡)和 ACTION_MEDIA_SHARED (如果设备作为大容量存储连接,它应该处理该操作,但事实并非如此)。
当检测到这两个操作时,我会发送一条错误消息并完成应用程序。
当我手动取出 SD 卡时,它可以工作,但不能作为大容量存储。
另外,在每一帧上,我都会使用此方法检查 SD 卡是否在每一帧的设备上。
public int hasSDCard()
{
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
return 1;
return 0;
}
当为 0 时,它没有 SD 卡,我也发送错误消息并完成应用程序。
但仍然无法在 USB 大容量存储模式下工作。
我的问题是:如何检测设备何时连接为 USB 海量存储?
我应该使用哪些类?
我可以实现哪些侦听器来调用我的错误消息活动并完成我的应用程序?
I developing a game with SD resources. When I connect the device as MASS STORAGE the device crash. It's because when I plug my device in the PC as Mass Storage the device doesn't have access to the SD card. So I tried different ways to solved.
Currently I'm implementing a BroadcastReceiver and I'm handling the actions ACTION_MEDIA_BAD_REMOVAL (if the SD card is removed manually) and ACTION_MEDIA_SHARED (it supposed to handle the action if the device is connected as Mass Storage, but it doesn't).
When both of these actions is detected I send an error message and I finish the app.
When I remove the SD card manually it works, but not as mass storage.
Also on every frame I check if the SD card is on the device on every frame with this method.
public int hasSDCard()
{
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
return 1;
return 0;
}
When is 0 it doesn't have SD card and that where I also send the error message and finish the app.
But stills doesn't work on USB Mass Storage mode.
My question are: How can I detect when the device is connected as USB Mass Storage ?
What classes should I use?
Which listeners I can implement to call my error message activity and finish my app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
BroadcastReceiver
类。首先为ACTION_MEDIA_MOUNTED
或ACTION_MEDIA_UNMOUNTED
意图注册一个 BroadcastReceiver。创建一个将重写 onReceive() 方法的类,该方法是处理安装/卸载的方法。http://developer.android.com/reference/android/content/Intent .html#ACTION_MEDIA_MOUNTED
作为额外信息,请阅读
ACTION_UMS_CONNECTED
的弃用http://developer.android.com/reference/android/content/Intent.html #ACTION_UMS_CONNECTED
You should use
BroadcastReceiver
class. At the begining register a BroadcastReceiver forACTION_MEDIA_MOUNTED
orACTION_MEDIA_UNMOUNTED
intent. Create a class that will overrideonReceive()
method, which is the method where you handle the mounting/unmounting.http://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_MOUNTED
As an extra info read the deprecation of
ACTION_UMS_CONNECTED
http://developer.android.com/reference/android/content/Intent.html#ACTION_UMS_CONNECTED