歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 掃描SDCard上的音樂文件以及監聽掃描事件

Android 掃描SDCard上的音樂文件以及監聽掃描事件

日期:2017/3/1 10:12:22   编辑:Linux編程

Android 掃描SDCard上的音樂文件以及監聽掃描事件

廣播接收機代碼:

  1. public class ScanSdReceiver extends BroadcastReceiver
  2. {
  3. @Override
  4. public void onReceive(Context context, Intent intent)
  5. {
  6. String action = intent.getAction();
  7. if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action))
  8. {
  9. //開始掃描,把你的代碼放這裡
  10. }
  11. else if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action))
  12. {
  13. //掃描結束,把你的代碼放這裡
  14. }
  15. }
  16. }

注冊廣播接收機,監聽SDcard掃描事件

  1. IntentFilter intentfilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
  2. intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
  3. intentfilter.addDataScheme("file");
  4. ScanSdReceiver scanSdReceiver = new ScanSdReceiver();
  5. registerReceiver(scanSdReceiver, intentfilter);

掃描SDCard

  1. sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
  2. Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath())));
Copyright © Linux教程網 All Rights Reserved