2014-02-11 99 views
0

我使用下面的代碼來刷新SD卡我如何知道媒體掃描已完成?

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
       Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 

我認爲這是一個發送廣播媒體XXXX接收器。我想在接收人完成工作後更新我的觀點。我怎樣才能做到這一點?

回答

2

註冊在清單

<receiver android:name=".MediaScannerFinishedReceiver"> 
    <intent-filter> 
    <action android:name="android.intent.action.MEDIA_SCANNER_FINISHED"/> 
    </intent-filter> 
</receiver> 

MEDIA_SCANNER_FINISHED行動,那麼在你的活動課聽同樣的動作

BroadcastReceiver mMediaScannerReceiver; 
public void registerMediaScannerListener() { 
      if (mMediaScannerReceiver == null) { 
        mMediaScannerReceiver = new BroadcastReceiver() { 
          @Override 
          public void onReceive(Context context, Intent intent) { 
            String action = intent.getAction(); 
            if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) { 
              System.out.println("I'm here!"); 
            } 
          } 
        }; 
        IntentFilter iFilter = new IntentFilter(); 
        iFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED); 
        registerReceiver(mMediaScannerReceiver, iFilter); 
      } 
    } 
+0

謝謝約翰..讓我來試試你的代碼... – saa

+0

Downvoted因爲接收者正在代碼和清單中註冊,這兩者都不需要。 – Meanman

相關問題