You need to listen for ACTION_MEDIA_REMOVED and ACTION_MEDIA_MOUNTED. Create a receiver and listen for this action.
EDIT:
In your manifest file add this
<receiver android:name=".MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_REMOVED" />
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
</receiver>
then create a class MyReceiver which will extend BroadcastReceiver and then catch these actions and perform what you want to do.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…