2011-05-16 191 views
2

我正在使用sendBroadcast進行媒體文件掃描。然後我需要等到sendBroadcast完成後再完成。我如何在Android中執行此操作?如何等待sendBroadcast完成

我知道我可以用一個簡單的,而這裏的邏輯,但我正在尋找一個更好的辦法

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

// wait here till till do something completed 

接收機

private BroadcastReceiver mediaScanner = new BroadcastReceiver(){ 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
       if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) { 
        // Do some thing here. 
       } 
    } 
} 

回答

0

以下是檢查媒體掃描儀是否正在運行的代碼。

public static final boolean isMediaScannerScanning(final ContentResolver cr) { 
    boolean result = false; 
    final Cursor cursor = query(cr, MediaStore.getMediaScannerUri(), new String[] { MediaStore.MEDIA_SCANNER_VOLUME }, null, 
      null, null); 
    if (cursor != null) { 
     if (cursor.getCount() == 1) { 
      cursor.moveToFirst(); 
      result = "external".equals(cursor.getString(0)); 
     } 
     cursor.close(); 
    } 
    return result; 
} 

我這裏張貼了答案 http://androidbridge.blogspot.com/2012/06/how-to-check-whether-android-media.html

+0

爲什麼不公佈答案** **在這裏? – 2012-06-28 12:17:42