1. 程式人生 > >Android 檢測 USB 拔插事件

Android 檢測 USB 拔插事件

靜態註冊 :

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.BluteToothBroadCast"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion=
"21"/> <!-- USB 許可權 --> <!--宣告使用usb--> <uses-permission android:name="android.hardware.usb.host" /> <uses-permission android:name="android.hardware.usb.accessory" /> <!-- 藍芽相關許可權 --> <uses-permission android:name="android.permission.BLUETOOTH"
/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/> <!-- 如果掃描到可用的裝置,還會觸發廣播 String ACTION_FOUND = "android.bluetooth.device.action.FOUND" //屬於類 `BluetoothDevice`對應許可權 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> <activity android:name="MyActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <receiver android:name=".MyStaticReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="staticBroadcast"></action> </intent-filter> </receiver> <receiver android:name=".USBReceiver"> <intent-filter android:priority="200"> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/> </intent-filter> <intent-filter android:priority="200"> <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.MEDIA_MOUNTED"/> <data android:scheme="file"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.MEDIA_EJECT"/> <data android:scheme="file"/> </intent-filter> </receiver> <receiver android:name=".USBReceiver2"> <intent-filter android:priority="100"> <action android:name="android.hardware.usb.action.USB_STATE"/> </intent-filter></receiver> </application> </manifest>

USBReceiver 檔案 :

public class USBReceiver  extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
      String  action =intent.getAction();
        Toast.makeText(context , "USB" , Toast.LENGTH_SHORT).show();
      if(action.equals(UsbManager.ACTION_USB_DEVICE_ATTACHED))
      //  if(action.equals("android.hardware.usb.action.USB_DEVICE_ATTACHED"))
        {

            Log.i("USBReceiver","靜態廣播接收器接收到裝置插入");
        }
        //"android.hardware.usb.action.USB_DEVICE_DETACHED"
        else  if(action.equals("android.hardware.usb.action.USB_DEVICE_DETACHED"))
        //else  if(action.equals(UsbManager.ACTION_USB_DEVICE_DETACHED))
        {
            Log.i("USBReceiver","靜態廣播接收器接收到裝置拔出");
        }
    }
}
USBReceiver2:

  

public class USBReceiver2 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if(intent.getAction().equals("android.hardware.usb.action.USB_STATE")){
            if (intent.getExtras().getBoolean("connected")){
                // usb 插入
                Toast.makeText(context, "USB插入", Toast.LENGTH_LONG).show();
            }else{
                //   usb 拔出
                Toast.makeText(context, "USB拔出", Toast.LENGTH_LONG).show();
            }
        }
    }
}