Android:如何知道SD卡何时被卸载

发布于 2024-11-28 09:39:57 字数 201 浏览 2 评论 0原文

我正在开发一个从 sd 卡读取媒体并播放它的应用程序。但是,当 sd 卡从设备中物理移除或设备连接到计算机并且 sd 卡被移除时,会发生某些事件。安装在计算机上。每当移除 SD 卡时,都会生成一条通知,并可以在通知栏中看到。我希望我的应用程序能够响应这些通知。例如,每当用户物理移除卡或将其连接到计算机时,我希望我的应用程序显示类似“SD 卡已移除”的消息。有什么方法可以响应这些通知吗?

I am working on an application that reads media from the sd-card and plays it.. However, there are certain events when the sd-card is physically removed from the device or the device is attached to a computer and the sd-card is mounted on the computer.. Whenever the sd-card is removed, a notification is generated and can be seen in the notification bar. I want my application to respond to those notifications. For example, whenever the user physically removes the card or attaches it to the computer, I want my application to display a message like "sd card removed".. Is there any way to respond to these notifications??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半暖夏伤 2024-12-05 09:39:57

我希望我的应用程序能够响应这些通知。

不,你不知道。您想要响应广播意图,该广播也用于创建这些通知

查看Intent 类的文档 ,位于 ACTION_MEDIA_* 广播操作集。

I want my application to respond to those notifications.

No, you don't. You want to respond to the broadcast Intents that are also used to create those Notifications.

Look at the documentation for the Intent class, at the ACTION_MEDIA_* set of broadcast actions.

盛夏尉蓝 2024-12-05 09:39:57

我知道问题很旧,但我最终在这里寻找这段代码,所以就在这里。

在manifest.xml中注册接收器:

<receiver android:name="it.myapp.receiver.OnMediaMountedReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <data android:scheme="file"/>
    </intent-filter>
</receiver>

然后创建接收器类:

public class OnMediaMountedReceiver extends BroadcastReceiver {

private static final String TAG = OnMediaMountedReceiver.class.getSimpleName();

@Override
public void onReceive(Context arg0, Intent intent) {
    Log.i(TAG, "Media mounted!");
    if (intent != null && intent.getData() != null) {
        Log.i(TAG, "Media path: " + intent.getDataString());
    }
    }
}

I know question is old but I ended up here looking for this code, so here it is.

In the manifest.xml register the receiver:

<receiver android:name="it.myapp.receiver.OnMediaMountedReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <data android:scheme="file"/>
    </intent-filter>
</receiver>

Then create receiver class:

public class OnMediaMountedReceiver extends BroadcastReceiver {

private static final String TAG = OnMediaMountedReceiver.class.getSimpleName();

@Override
public void onReceive(Context arg0, Intent intent) {
    Log.i(TAG, "Media mounted!");
    if (intent != null && intent.getData() != null) {
        Log.i(TAG, "Media path: " + intent.getDataString());
    }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文