简体   繁体   中英

how to wait for sendBroadcast to finish

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 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. 
                }
    }
}

您可以在Java中寻找Observable模式 ,它符合您的意愿

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM