import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
public class USBDiskReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String path = intent.getData().getPath();
if (!TextUtils.isEmpty(path)) {
if ("android.intent.action.MEDIA_MOUNTED".equals(action)) {
System.out.println("U盘路径----"+ path);
}
if ("android.intent.action.MEDIA_REMOVED".equals(action)) {
System.out.println("U盘拨出----");
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 在清单文件(AndroidManifest.xml)中注册
<receiver android:name=".USBDiskReceiver">
<intent-filter android:priority="999" >
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<action android:name="android.intent.action.MEDIA_UNMOUNTED" />
<action android:name="android.intent.action.MEDIA_REMOVED" />
<data android:scheme="file" />
</intent-filter>
</receiver>