欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 利用广播监听usb连接状态(变化情况)

程序员文章站 2022-06-19 22:40:47
废话不多说了,直接给大家贴代码了,具体代码如下所示: package com.lgs.test.testcode.receiver; import andro...

废话不多说了,直接给大家贴代码了,具体代码如下所示:

package com.lgs.test.testcode.receiver; 
import android.content.broadcastreceiver; 
import android.content.context; 
import android.content.intent; 
import android.content.intentfilter; 
import android.widget.toast; 
/** 
 * created by administrator on 2017/6/26. 
 */ 
public class usbconnect { 
  private final static string action = "android.hardware.usb.action.usb_state"; 
  public void start(context context) { 
    intentfilter filter = new intentfilter(); 
    filter.addaction(action); 
    context.registerreceiver(usbroadcastreceiver, filter); 
  } 
  broadcastreceiver usbroadcastreceiver = new broadcastreceiver() { 
    @override 
    public void onreceive(context context, intent intent) { 
      string action = intent.getaction(); 
      toast.maketext(context, "aciton =" + action, toast.length_short).show(); 
      if (action.equals(action)) { 
        boolean connected = intent.getextras().getboolean("connected"); 
        toast.maketext(context, "aciton =" + connected, toast.length_short).show(); 
        if (connected) { 
        } else { 
        } 
      } 
    } 
  }; 
} 

以上所述是小编给大家介绍的android 利用广播监听usb连接状态,希望对大家有所帮助