In the android system, when the SD card is attached to a computer and the recording in the voice memo is deleted manually, the data in the corresponding database also needs to be modified. In this case, you need to listen to the Mount and inherit the BroadcastReceiver class to implement the onRecieve (Context context, Intent inten) method. The Code is as follows:
public void onReceive(Context context, Intent intent) {String action = intent.getAction();if (action.equals(Intent.ACTION_MEDIA_EJECT)) {//TODO:System.out.println("-------------------> mount ACTION_MEDIA_EJECT");Cursor c = MemorDataBaseHelper.queryMemor(context);while(c.moveToNext()){String path = c.getString(MemorDataBaseHelper.DATA_COLUMN_INDEX);File f = new File(path);if(!f.exists()){System.out.println(f+"the file does not exist");MemorDataBaseHelper.deleteMemor(context, c.getString(MemorDataBaseHelper.ID_COLUMN_INDEX));}}c.close();} else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {//TODO: System.out.println("-------------------> mount ACTION_MEDIA_MOUNTED");Cursor c = MemorDataBaseHelper.queryMemor(context);while(c.moveToNext()){String path = c.getString(MemorDataBaseHelper.DATA_COLUMN_INDEX);File f = new File(path);if(!f.exists()){System.out.println(f+"the file does not exist");MemorDataBaseHelper.deleteMemor(context, c.getString(MemorDataBaseHelper.ID_COLUMN_INDEX));}}c.close();}}
At this time, you must register in Manifest:
<receiver android:name=".ExternalStorageListener"> <intent-filter> <action android:name="android.intent.action.MEDIA_EJECT" /> <action android:name="android.intent.action.MEDIA_MOUNTED" /> <data android:scheme="file"/> </intent-filter> </receiver>