文章出處

首先,新建一個類,繼承于BroadcastReceiver,然后去配置Manifest.xml這就不用說了,

注意配置Manifest.xml時候的一些細節

必須加上<data android:scheme="file"/>

1 <receiver android:name=".SdReceicer">
2             <intent-filter>
3                 <action android:name="android.intent.action.MEDIA_MOUNTED"/>
4                 <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
5                 <data android:scheme="file"/>
6             </intent-filter>
7         </receiver>

然后簡單了兩句判斷就行了

 1 public class SdReceicer extends BroadcastReceiver {
 2     @Override
 3     public void onReceive(Context context, Intent intent) {
 4         String action = intent.getAction();//得到執行的是哪個action
 5         if ("android.intent.action.MEDIA_MOUNTED".equals(action)){
 6             Toast.makeText(context,"SD卡加載了",Toast.LENGTH_SHORT).show();
 7         }else if ("android.intent.action.MEDIA_UNMOUNTED".equals(action)){
 8             Toast.makeText(context,"SD卡被卸載了",Toast.LENGTH_SHORT).show();
 9         }
10     }
11 }

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()