android:如何收听“sd卡意外删除”

发布于 2024-11-28 14:14:00 字数 86 浏览 4 评论 0 原文

我有一个使用 SD 卡内容的程序。我想听不同的状态,例如 SD 卡已安装或 SD 卡意外移除。我该怎么办呢。一个例子会有很大的帮助。

感谢大家

I have a program that uses content from sd-card. I want to listen to different states like sd-card mounted or sd-card removed unexpectedly. How can I do so. An example would be of a great help.

Thanks to all

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

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

发布评论

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

评论(4

若能看破又如何 2024-12-05 14:14:00

您需要监听 ACTION_MEDIA_REMOVEDACTION_MEDIA_MOUNTED。创建一个接收器并监听此操作。

编辑:

在您的清单文件中添加此内容

<receiver android:name=".MyReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_REMOVED" />
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <data android:scheme="file" />
    </intent-filter>
</receiver>

,然后创建一个类 MyReceiver ,它将扩展 BroadcastReceiver ,然后捕获这些操作并执行您想要执行的操作。

You need to listen for ACTION_MEDIA_REMOVED and ACTION_MEDIA_MOUNTED. Create a receiver and listen for this action.

EDIT:

In your manifest file add this

<receiver android:name=".MyReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_REMOVED" />
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <data android:scheme="file" />
    </intent-filter>
</receiver>

then create a class MyReceiver which will extend BroadcastReceiver and then catch these actions and perform what you want to do.

凉薄对峙 2024-12-05 14:14:00

在Manifest中创建一个接收者:

    <receiver android:name=".ExternalSDcardRemoved">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_EJECT" />
            <data android:scheme="file" />
        </intent-filter>
    </receiver>

以及对应的类文件:

public class ExternalSDcardRemoved extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        // SD card removed
    }
}

Create a receiver in Manifest:

    <receiver android:name=".ExternalSDcardRemoved">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_EJECT" />
            <data android:scheme="file" />
        </intent-filter>
    </receiver>

And a corresponding class file:

public class ExternalSDcardRemoved extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        // SD card removed
    }
}
墨离汐 2024-12-05 14:14:00

您可以使用这样的方法:

    static boolean checkSdCardStatus(final Activity activity) {

    String status = Environment.getExternalStorageState();
    //the SD Card is mounted as read-only, but we require it to be writable.
    if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        UIMethods.showFinalAlert(activity, R.string.sdcard_readonly);
        return false;
    }
    //your handset is mounted as a USB device
    if (status.equals(Environment.MEDIA_SHARED)) {
        UIMethods.showFinalAlert(activity, R.string.sdcard_shared);
        return false;
    }
    //no SD Card inserted
    if (!status.equals(Environment.MEDIA_MOUNTED)) {
        UIMethods.showFinalAlert(activity, R.string.no_sdcard);
        return false;
    }

    return true;
}

并在 Activity.onStart()Activity.onResume() 中调用此方法。

You can use something like this:

    static boolean checkSdCardStatus(final Activity activity) {

    String status = Environment.getExternalStorageState();
    //the SD Card is mounted as read-only, but we require it to be writable.
    if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        UIMethods.showFinalAlert(activity, R.string.sdcard_readonly);
        return false;
    }
    //your handset is mounted as a USB device
    if (status.equals(Environment.MEDIA_SHARED)) {
        UIMethods.showFinalAlert(activity, R.string.sdcard_shared);
        return false;
    }
    //no SD Card inserted
    if (!status.equals(Environment.MEDIA_MOUNTED)) {
        UIMethods.showFinalAlert(activity, R.string.no_sdcard);
        return false;
    }

    return true;
}

And call this method in Activity.onStart() or in Activity.onResume().

日暮斜阳 2024-12-05 14:14:00

感谢@PravinCG

这是完整的代码。

SDCardBroadcastReceiver.java 代码

public class SDCardBroadcastReceiver extends BroadcastReceiver {


    private static final String ACTION_MEDIA_REMOVED = "android.intent.action.MEDIA_REMOVED";
    private static final String ACTION_MEDIA_MOUNTED = "android.intent.action.MEDIA_MOUNTED";
    private static final String MEDIA_BAD_REMOVAL = "android.intent.action.MEDIA_BAD_REMOVAL";
    private static final String MEDIA_EJECT = "android.intent.action.MEDIA_EJECT";
    private static final String TAG = "SDCardBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {



        Log.i(TAG, "Intent recieved: " + intent.getAction());

        if (intent.getAction() == ACTION_MEDIA_REMOVED) {

            Log.e(TAG, "ACTION_MEDIA_REMOVED called");

            // For bundle Extras do like below
//            Bundle bundle = intent.getExtras();
//            if (bundle != null) {
//
//            }
        }else if (intent.getAction() == ACTION_MEDIA_MOUNTED){

            Log.e(TAG, "ACTION_MEDIA_MOUNTED called");

        }else if(intent.getAction() == MEDIA_BAD_REMOVAL){

            Log.e(TAG, "MEDIA_BAD_REMOVAL called");

        }else if (intent.getAction() == MEDIA_EJECT){

            Log.e(TAG, "MEDIA_EJECT called");

        }
    }
}

,这是我的 manifest.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="genetechsolutions.sdcardmountlistner">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <receiver android:name=".SDCardBroadcastReceiver" >
            <intent-filter>
                <data android:scheme="file" />
                <action android:name="android.intent.action.MEDIA_REMOVED" />
                <action android:name="android.intent.action.MEDIA_MOUNTED" />
                <action android:name="android.intent.action.MEDIA_EJECT" />
                <action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

Thanks to @PravinCG

Here is the complete code.

SDCardBroadcastReceiver.java code

public class SDCardBroadcastReceiver extends BroadcastReceiver {


    private static final String ACTION_MEDIA_REMOVED = "android.intent.action.MEDIA_REMOVED";
    private static final String ACTION_MEDIA_MOUNTED = "android.intent.action.MEDIA_MOUNTED";
    private static final String MEDIA_BAD_REMOVAL = "android.intent.action.MEDIA_BAD_REMOVAL";
    private static final String MEDIA_EJECT = "android.intent.action.MEDIA_EJECT";
    private static final String TAG = "SDCardBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {



        Log.i(TAG, "Intent recieved: " + intent.getAction());

        if (intent.getAction() == ACTION_MEDIA_REMOVED) {

            Log.e(TAG, "ACTION_MEDIA_REMOVED called");

            // For bundle Extras do like below
//            Bundle bundle = intent.getExtras();
//            if (bundle != null) {
//
//            }
        }else if (intent.getAction() == ACTION_MEDIA_MOUNTED){

            Log.e(TAG, "ACTION_MEDIA_MOUNTED called");

        }else if(intent.getAction() == MEDIA_BAD_REMOVAL){

            Log.e(TAG, "MEDIA_BAD_REMOVAL called");

        }else if (intent.getAction() == MEDIA_EJECT){

            Log.e(TAG, "MEDIA_EJECT called");

        }
    }
}

and here is my manifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="genetechsolutions.sdcardmountlistner">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <receiver android:name=".SDCardBroadcastReceiver" >
            <intent-filter>
                <data android:scheme="file" />
                <action android:name="android.intent.action.MEDIA_REMOVED" />
                <action android:name="android.intent.action.MEDIA_MOUNTED" />
                <action android:name="android.intent.action.MEDIA_EJECT" />
                <action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
            </intent-filter>
        </receiver>

    </application>

</manifest>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文