Android 插拔sd廣播

來源:互聯網
上載者:User

項目中要做媒體掃描,需要檢測sd插拔事件。寫了個demo分析下。

[java]
mReceiver = new BroadcastReceiver() { 
            @Override 
            public void onReceive(Context context, Intent intent) { 
                onReceiveMediaBroadcast(intent); 
            } 
        }; 
 mReceiver = new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
                 onReceiveMediaBroadcast(intent);
             }
         };
[java]
//onReceive方法:  
    private void onReceiveMediaBroadcast(Intent intent) { 
            String action = intent.getAction(); 
            if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) { 
                Log.i(Intent.ACTION_MEDIA_MOUNTED); 
            } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) { 
                Log.i(Intent.ACTION_MEDIA_UNMOUNTED); 
            } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) { 
                Log.i(Intent.ACTION_MEDIA_SCANNER_STARTED); 
            } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) { 
                Log.i(Intent.ACTION_MEDIA_SCANNER_FINISHED); 
            } else if (action.equals(Intent.ACTION_MEDIA_BAD_REMOVAL)) { 
                Log.i(Intent.ACTION_MEDIA_BAD_REMOVAL); 
            } 
            else if (action.equals(Intent.ACTION_MEDIA_EJECT)) { 
                Log.i(Intent.ACTION_MEDIA_EJECT); 
            } 
            else if (action.equals(Intent.ACTION_MEDIA_REMOVED)) { 
                Log.i(Intent.ACTION_MEDIA_REMOVED); 
            } 
        } 
//onReceive方法:
 private void onReceiveMediaBroadcast(Intent intent) {
         String action = intent.getAction();
         if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
          Log.i(Intent.ACTION_MEDIA_MOUNTED);
         } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) {
          Log.i(Intent.ACTION_MEDIA_UNMOUNTED);
         } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) {
          Log.i(Intent.ACTION_MEDIA_SCANNER_STARTED);
         } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
          Log.i(Intent.ACTION_MEDIA_SCANNER_FINISHED);
         } else if (action.equals(Intent.ACTION_MEDIA_BAD_REMOVAL)) {
          Log.i(Intent.ACTION_MEDIA_BAD_REMOVAL);
         }
         else if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
          Log.i(Intent.ACTION_MEDIA_EJECT);
         }
         else if (action.equals(Intent.ACTION_MEDIA_REMOVED)) {
          Log.i(Intent.ACTION_MEDIA_REMOVED);
         }
     }
[java]
//註冊廣播接收器:  
        IntentFilter intentFilter = new IntentFilter(); 
        intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED); 
        intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED); 
        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED); 
        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED); 
        intentFilter.addAction(Intent.ACTION_MEDIA_EJECT); 
        intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL); 
        intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED); 
        intentFilter.addDataScheme("file"); 
        registerReceiver(mReceiver, intentFilter); 
 //註冊廣播接收器:
         IntentFilter intentFilter = new IntentFilter();
         intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
         intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
         intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
         intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
         intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
         intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
         intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
         intentFilter.addDataScheme("file");
         registerReceiver(mReceiver, intentFilter);
log資訊:
插卡:系統打出來的狀態,{removed}->{unmounted}->{checking}->{mounted}
    我的程式列印資訊,android.intent.action.MEDIA_UNMOUNTED->android.intent.action.MEDIA_MOUNTED
                  ->android.intent.action.MEDIA_SCANNER_STARTED->android.intent.action.MEDIA_SCANNER_FINIDHED
拔卡:系統打出來的狀態,{mounted}->{umounted}->{removed}
    我的程式列印資訊,android.intent.action.MEDIA_EJECT->android.intent.action.MEDIA_UNMOUNTED
                  ->android.intent.action.MEDIA_REMOVED

 

摘自  痛而不言,笑而不語
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.