Skip to content

Instantly share code, notes, and snippets.

@kwong93
Created September 7, 2017 21:06
Show Gist options
  • Save kwong93/0ac3a56998b74acbf4c61736bc555a26 to your computer and use it in GitHub Desktop.
Save kwong93/0ac3a56998b74acbf4c61736bc555a26 to your computer and use it in GitHub Desktop.
How to listen for sd card mount and unmount
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", intent.getAction());
}
};
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addDataScheme("file");
registerReceiver(broadcastReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment