80,356
社区成员
发帖
与我相关
我的任务
分享
SdcardReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_MEDIA_SHARED)){
do somethings
}
else if(intent.getAction().equals(Intent.ACTION_MEDIA_EJECT)){
do somethings
}
....
}
创建时注册广播
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
//intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
//intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
//intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.addDataScheme(“file”);
registerReceiver(SdcardReceiver , intentFilter);
程序结束
unregisterReceiver(SdcardReceiver);
File file = new File(
android.os.Environment.getExternalStorageDirectory(),
"Text.log.txt");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}