You can implement a BroadcastReceiver and wait for the following actions:
android.intent.action.MEDIA_MOUNTED
android.intent.action.MEDIA_EJECT
AndroidManifest.xml:
<receiver
android:name=".MyReceiver"
android:exported="false" >
<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>
MyReceiver.java:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ((Intent.ACTION_MEDIA_EJECT.equals(intent.getAction())
|| Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction()))
&& Environment.isExternalStorageRemovable()) {
// Handle the SD card state changed
}
}
}