我怎么知道媒体扫描完成了?

时间:2014-02-11 11:58:07

标签: android android-broadcast

我使用以下代码刷新SDCard

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())));

我认为它正在向媒体xxxx接收器发送一个广播。我希望在接收者完成工作后更新我的观点。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

在清单

中注册MEDIA_SCANNER_FINISHED操作
<receiver android:name=".MediaScannerFinishedReceiver">
   <intent-filter>
   <action android:name="android.intent.action.MEDIA_SCANNER_FINISHED"/>
   </intent-filter>
</receiver>

然后在你的活动类中听取同样的动作

BroadcastReceiver mMediaScannerReceiver;
public void registerMediaScannerListener() {
            if (mMediaScannerReceiver == null) {
                    mMediaScannerReceiver = new BroadcastReceiver() {
                            @Override
                            public void onReceive(Context context, Intent intent) {
                                    String action = intent.getAction();
                                    if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
                                            System.out.println("I'm here!");
                                    }
                            }
                    };
                    IntentFilter iFilter = new IntentFilter();
                    iFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
                    registerReceiver(mMediaScannerReceiver, iFilter);
            }
    }