当前位置: 移动技术网 > IT编程>移动开发>Android > Android 利用广播监听usb连接状态(变化情况)

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

ca137,禽类,故事大王

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

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连接状态,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网