广播接收机代码:
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())));