Android 掃描SDCard上的音樂文件以及監聽掃描事件
廣播接收機代碼:
- public class ScanSdReceiver extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- String action = intent.getAction();
- if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action))
- {
- //開始掃描,把你的代碼放這裡
- }
- else if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action))
- {
- //掃描結束,把你的代碼放這裡
- }
- }
- }
注冊廣播接收機,監聽SDcard掃描事件
- IntentFilter intentfilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
- intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
- intentfilter.addDataScheme("file");
- ScanSdReceiver scanSdReceiver = new ScanSdReceiver();
- registerReceiver(scanSdReceiver, intentfilter);
掃描SDCard
- sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
- Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath())));