问题
I have two activity: Main and Processing. in the Processing activity do an edit operation on the file names in a folder, and then start a scan to update via broadcast, and I check when the scan finish.
//end batch renaming operation
//start updating media store library
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+Environment.getExternalStorageDirectory())));
//show toast "updating"
op_media_mounted.show();
//catch when updaitng op ends
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_FINISHED);
filter.addDataScheme("file");
BroadcastReceiver mReceiver = (new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
Toast.makeText(frm_elabora.this, "Scan complete.", Toast.LENGTH_SHORT).show();
}
}
});
registerReceiver(mReceiver, filter);
I would like to pass the Activity prevent processing if the scan is running. I can check first if the scan is not running, then launch the second processing activity? as this.
Button btnstart = (Button)findViewById(R.id.btn_start);
btnstart.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if ( there_isnt_scan_for_new_media_file )
startActivity(intent_processing);
else{
wait_until_ACTION_MEDIA_SCANNER_FINISHED
startActivity(intent_processing);
}
I need to check this, because if i rename the files while android running a scan, when I edit the files in the processing activity, in the end I find other files in the folder with size 0 bytes, as before the change. so I have to wait that the scan finish. i can't find a solution.
UPDATES: i've found this
public static Uri getMediaScannerUri ()
Since: API Level 1
Uri for querying the state of the media scanner.
i try this
//send broadcast
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" +
Environment.getExternalStorageDirectory())));
//show media scanner state
Toast.makeText(frm_elabora.this,
MediaStore.getMediaScannerUri().toString(),
Toast.LENGTH_SHORT).show();
and i get "content://media/none/media_scanner" always... if i ask getMediaScannerUri() after sent ACTION_MEDIA_MOUNTED, i should get something different from "content://media/none/media_scanner" ???
回答1:
did you try MEDIA_SCANNER_VOLUME
Usually when the media scanner is running this column is not null.
来源:https://stackoverflow.com/questions/9647478/how-to-detect-if-is-running-the-scan-for-new-media-files-on-android-in-backgrou