Android监听SDcard插拔广播
作者:
APP福星高照 | 来源:发表于
2022-06-06 10:47 被阅读0次// 声明监听广播
BroadcastReceiver mountReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Uri data = intent.getData();
String path = data.getPath();
File file = new File(path);
// doing
}
};
// 注册接受广播
void register() {
try {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.setPriority(1000);
intentFilter.addDataScheme("file");
getActivity().registerReceiver(mountReceiver, intentFilter);
} catch (Exception e) {
e.printStackTrace();
}
}
// 注销
void unregister() {
try {
getActivity().unregisterReceiver(mountReceiver);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
unregister();
}
本文标题:Android监听SDcard插拔广播
本文链接:https://www.haomeiwen.com/subject/wcqjmrtx.html
网友评论