首页 > 代码库 > android 监听 USB 拔插广播消息

android 监听 USB 拔插广播消息

Java:

 1 package com.example.communication; 2  3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.widget.Toast; 7  8 public class USBBroadcastReceiver extends BroadcastReceiver{ 9     @Override10     public void onReceive(Context context, Intent intent) {11         // TODO Auto-generated method stub12         if(intent.getAction().equals("android.hardware.usb.action.USB_STATE")){13              if (intent.getExtras().getBoolean("connected")){14                 // usb 插入15                  Toast.makeText(context, "插入", Toast.LENGTH_LONG).show();16               }else{17                //   usb 拔出18                   Toast.makeText(context, "拔出", Toast.LENGTH_LONG).show();19               }20         }21     }22 }

AndroidManifest.xml

1 <receiver android:name=".USBBroadcastReceiver">  2    <intent-filter android:priority="800">  3        <action android:name="android.hardware.usb.action.USB_STATE"/>4     </intent-filter>  5 </receiver>