Receive broadcast when SD card unmount android

I need to get a broadcast reception when the media unmounts ie before it is unmounted.

So that I can save the file to the SD card before unmounting the Media.

I used android.intent.action.MEDIA_UNMOUNTED.

<intent-filter> <action android:name="android.intent.action.MEDIA_UNMOUNTED" /> <data android:scheme="file" /> <category android:name="android.intent.category.HOME" /> </intent-filter> 

But it was received after the media was turned off, and then I could not save the file.

I also tried MEDIA_EJECT, MEDIA_SHARED, all obtained after Media Unmounted.

Is there any other way to save the file. I do not want to use Thread to save the file.

+6
source share
3 answers

Use ACTION_MEDIA_EJECT: The user has expressed a desire to remove external media. Applications should close all files that they opened at the mount point when they receive this intention. The path to the mount point for the removed media is contained in the Intent.mData field.

+3
source

try android.intent.action.ACTION_MEDIA_UNMOUNTED it should get broadcast. but I'm not sure. never been used.

+1
source

You can only receive a broadcast after it has been completed or completed, so you won’t be able to save it to the SD card.

Invite you to temporarily save the file in the internal storage, and then when the SD card appears, move it back to the SD card.

0
source

Source: https://habr.com/ru/post/907634/


All Articles