I am doing a media file scan using sendBroadcast. Then I need to wait till it's complete after doing sendBroadcast. How do I do this in Android ?
I know I can use a simple while logic here but I am looking for a better approach
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
// wait here till till do something completed
Receiver
private Broadca开发者_C百科stReceiver 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.
}
}
}
You can look for the Observable pattern in Java, it matchs your will
Here is the code to check whether media scanner is running.
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;
}
I posted the answer here http://androidbridge.blogspot.com/2012/06/how-to-check-whether-android-media.html
精彩评论