Android monitors the mounting and unmounting of SD cards

Source: Internet
Author: User
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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.