Android 扫描SDCard上的音乐文件以及监听扫描事件
16lz
2021-01-23
From:http://blog.csdn.net/AMinfo/article/details/7875358
广播接收机代码:
[java] view plain copy print ?- publicclassScanSdReceiverextendsBroadcastReceiver
- {
- @Override
- publicvoidonReceive(Contextcontext,Intentintent)
- {
- Stringaction=intent.getAction();
- if(Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action))
- {
- //开始扫描,把你的代码放这里
- }
- elseif(Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action))
- {
- //扫描结束,把你的代码放这里
- }
- }
- }
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扫描事件
[java] view plain copy print ?- IntentFilterintentfilter=newIntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
- intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
- intentfilter.addDataScheme("file");
- ScanSdReceiverscanSdReceiver=newScanSdReceiver();
- registerReceiver(scanSdReceiver,intentfilter);
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
[java] view plain copy print ?- sendBroadcast(newIntent(Intent.ACTION_MEDIA_MOUNTED,
- Uri.parse("file://"+Environment.getExternalStorageDirectory().getAbsolutePath())));
更多相关文章
- Android结束进程的方法
- Android拒绝来电的实现--ITelephony类的反射(结束电话)
- android 结束进程
- 博文视点大讲堂35期-It's Android Time:程序员创富有道! 圆满结束