android_47_BroadcastReceiver_SD卡状态监测

来源:互联网 发布:google云端硬盘知乎 编辑:程序博客网 时间:2024/05/16 11:46

效果:


清单:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.sg31.mediastatusreceiver"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="21" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        //注册广播接收者        <receiver android:name=".MediaStatusReceiver">            <intent-filter >                <action android:name="android.intent.action.MEDIA_MOUNTED"/>                <action android:name="android.intent.action.MEDIA_REMOVED"/>                <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>                <data android:scheme="file"/>            </intent-filter>        </receiver>    </application></manifest>


布局:


广播接收者:

package com.sg31.mediastatusreceiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MediaStatusReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {//判断收到的到底是什么广播        String action = intent.getAction();        if("android.intent.action.MEDIA_MOUNTED".equals(action)){            Toast.makeText(context, "SD卡可用", 0).show();        }        else if("android.intent.action.MEDIA_REMOVED".equals(action)){            Toast.makeText(context, "SD卡拔出", 0).show();        }        else if("android.intent.action.MEDIA_UNMOUNTED".equals(action)){            Toast.makeText(context, "SD卡不可用", 0).show();        }}}


控制器:


0 0
原创粉丝点击