Android BroadcastReceiver获取所有SDCARD状态

时间:2015-06-22 06:14:17

标签: android

我尝试使用BroadcastReceiver获取所有SD卡状态,但代码无法正常工作。更改SD卡状态后,BroadcastReceiver不会提供全面的日志,例如打开(关闭)USB存储。有什么想法吗?

表现接收者行动:

<receiver android:name=".Broadcasts.BroadcastChangeSDCardStatus">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_REMOVED"/>
        <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
        <action android:name="android.intent.action.MEDIA_EJECT"/>
        <action android:name="android.intent.action.MEDIA_MOUNTED"/>
    </intent-filter>
</receiver>

或者这个接收者定义:

<receiver android:name=".Broadcasts.BroadcastChangeSDCardStatus">
    <intent-filter>
        <action android:name="android.intent.action.ACTION_MEDIA_BAD_REMOVAL"/>
        <action android:name="android.intent.action.ACTION_MEDIA_EJECT"/>
        <action android:name="android.intent.action.ACTION_MEDIA_REMOVED"/>
        <action android:name="android.intent.action.ACTION_MEDIA_UNMOUNTED"/>
    </intent-filter>
</receiver>

要检测的BroadcastReceiver类

public class BroadcastChangeSDCardStatus extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean status = getStorageStatus();
        Log.e("SDCARD Status: ", status + "");
    }
    public static boolean getStorageStatus() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return false;
        } else {
            return false;
        }
    }
}

3 个答案:

答案 0 :(得分:0)

<receiver android:name=".Broadcasts.BroadcastChangeSDCardStatus" >
<intent-filter>
    <action android:name="android.intent.action.MEDIA_REMOVED" />
    <action android:name="android.intent.action.MEDIA_MOUNTED" />
</intent-filter>

上面的代码应该可以工作,但正如你所说,你已经尝试过这样看起来你的接收者名称有问题。你的广播接收器没有注册。给接收者名称完整的包名,正确的类名

答案 1 :(得分:0)

您还需要将 System.Windows.Threading.DispatcherTimer dispatcherTimer; int t = 240000; // 4 minutes = 240,000 milliseconds public MainWindow() { InitializeComponent(); dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); //tick every 42 millisecond = tick 24 times in one second dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 42); } private void dispatcherTimer_Tick(object sender, EventArgs e) { // Go back 1 frame every 42 milliseconds (or 24 fps) t = t - 42; mePlayer.Position = TimeSpan.FromMilliseconds(t); } private void Window_Loaded(object sender, RoutedEventArgs e) { mePlayer.Play(); } private void Button_Click(object sender, RoutedEventArgs e) { // Pause and go to 4th minute of the video then start playing backward mePlayer.Pause(); mePlayer.Position = TimeSpan.FromMinutes(4); dispatcherTimer.Start(); } 设置为&#34;文件&#34; :
通过向您的接收器添加android:scheme来执行此操作,例如:

<data android:scheme="file" />

希望这可以帮助你

答案 2 :(得分:0)

我在添加此权限后立即解决我的问题:

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />