[英]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.