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     @Override
10     public void onReceive(Context context, Intent intent) {
11         // TODO Auto-generated method stub
12         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>

 

android 监听 USB 拔插广播消息,布布扣,bubuko.com

android 监听 USB 拔插广播消息

上一篇:IOS View之间传值


下一篇:Android:Layout_weight的深刻理解