[英]how to wait for sendBroadcast to finish
我正在使用sendBroadcast進行媒體文件掃描。 然后,我需要等到sendBroadcast完成后才能完成。 如何在Android中執行此操作?
我知道我可以在這里使用簡單的while邏輯,但我正在尋找更好的方法
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.
}
}
}
您可以在Java中尋找Observable模式 ,它符合您的意願
這是檢查媒體掃描儀是否正在運行的代碼。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.