我正在使用sendBroadcast进行媒体文件扫描。然后我需要等到sendBroadcast完成后再完成。我如何在Android中执行此操作?
我知道我可以在这里使用简单的逻辑,但我正在寻找更好的方法
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 :(得分:0)
您可以在Java中查找Observable pattern,它符合您的意愿
答案 1 :(得分: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