国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android Usb設備的監聽(Dev)外設端口的判定以及耳機的插拔

2019-10-21 21:33:21
字體:
來源:轉載
供稿:網友

最近在公司用到外設,需要判斷接入的外設的VendorId和ProductId,然后給大家說一下自己的學習成果把 ,首先我門可以通過android.hardware.usb.action.USB_STATE監聽自己的Usb連接的設備,只針對Usb設備。而想要監聽外部設備的時候卻需要另外的兩個廣播進行監聽"android.hardware.usb.action.USB_DEVICE_ATTACHED""android.hardware.usb.action.USB_DEVICE_DETACHED"。要是想對耳機或者耳機的狀態進行監聽的時候需要的廣播是"android.intent.action.HEADSET_PLUG" 通過

int inttype=intent.getIntExtra("microphone",0)來獲取耳機是否有麥克風。inttype==0表示沒有耳機inttype==1表示有耳機

我個人的建議就是將一部分代碼(根據個人情況而定)放到服務里面,或者是Application里面。

import com.example.usbusb.utils.ToastUtils;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.widget.Toast;public class MainActivity extends Activity { //耳機的廣播 public static final String TAGLISTEN = "android.intent.action.HEADSET_PLUG"; //usb線的廣播 private final static String TAGUSB = "android.hardware.usb.action.USB_STATE"; //外設的廣播 public static final String TAGIN = "android.hardware.usb.action.USB_DEVICE_ATTACHED"; public static final String TAGOUT = "android.hardware.usb.action.USB_DEVICE_DETACHED";  private boolean BOOLEAN=false;  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);  //賽選器 IntentFilter filter = new IntentFilter(); //篩選的條件 filter.addAction(TAGIN); filter.addAction(TAGOUT); filter.addAction(TAGUSB); //注冊廣播 動態注冊 registerReceiver(receiver, filter); } /** * 創建廣播的類 */ BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) {  String action = intent.getAction();  //判斷外設  if (action.equals(TAGIN)) {  ToastUtils.shwotoast(context, "外設已經連接");  //Toast.makeText(context, "外設已經連接", Toast.LENGTH_SHORT).show();  }  if (action.equals(TAGOUT)) {  if (BOOLEAN) {   ToastUtils.shwotoast(context, "外設已經移除");   //Toast.makeText(context, "外設已經移除", Toast.LENGTH_SHORT).show();  }  }  //判斷存儲usb  if (action.equals(TAGUSB)) {  boolean connected = intent.getExtras().getBoolean("connected");  if (connected) {   ToastUtils.shwotoast(context, "USB 已經連接");   //Toast.makeText(MainActivity.this, "USB 已經連接",Toast.LENGTH_SHORT).show();    } else {   if (BOOLEAN) {   ToastUtils.shwotoast(context, "USB 斷開");   //Toast.makeText(MainActivity.this, "USB 斷開",Toast.LENGTH_SHORT).show();   }  }  }  //判斷耳機  if (action.equals(TAGLISTEN)) {  int intExtra = intent.getIntExtra("state", 0);  // state --- 0代表拔出,1代表插入  // name--- 字符串,代表headset的類型。  // microphone -- 1代表這個headset有麥克風,0則沒有  // int i=intent.getIntExtra("",0);  if (intExtra == 0) {   if (BOOLEAN) {   ToastUtils.shwotoast(context,"拔出耳機");   //Toast.makeText(context, "拔出耳機", Toast.LENGTH_SHORT).show();   }  }  if (intExtra == 1) {   ToastUtils.shwotoast(context, "耳機插入");   //Toast.makeText(context, "耳機插入", Toast.LENGTH_SHORT).show();   int intType = intent.getIntExtra("microphone", 0);   if (intType == 0) {   ToastUtils.shwotoast(context, "沒有麥克風");   //Toast.makeText(context, "沒有麥克風" + intType,Toast.LENGTH_SHORT).show();   }   if (intType == 1) {   ToastUtils.shwotoast(context,"有話筒" );   //Toast.makeText(context, "有話筒" + intType,Toast.LENGTH_SHORT).show();   }  }   }  BOOLEAN=true; } }; /** * 注銷廣播 */ protected void onDestroy() { unregisterReceiver(receiver); };}

ToastUtils工具類

import android.content.Context;import android.widget.Toast;public class ToastUtils { public static Toast toast=null; private ToastUtils toastUtils=new ToastUtils(); private ToastUtils(){}  public static void shwotoast(Context context,String msg){   if (toast==null) {  toast=Toast.makeText(context, msg, Toast.LENGTH_SHORT); }else {  if (toast!=null) {  toast.setText(msg);  } }   toast.show();  }}

下面的一個就是獲取每一個Id的端口號通過在Usb的廣播里面調用這個方法判斷是否是自己的設備,這樣就可完成自己想要的操作了(注意當看到設備的ID是以0x開頭的是十六位的 然后轉化成十進制的數就能看到自己的東西了)

import java.util.HashMap;import android.annotation.SuppressLint;import android.content.Context;import android.hardware.usb.UsbDevice;import android.hardware.usb.UsbManager;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.util.Log; public class MainActivity extends ActionBarActivity {  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);    HashMap<String, UsbDevice> map = usbManager.getDeviceList();    System.out.println("......................befor....................................");    for(UsbDevice device : map.values()){     System.out.println(".......one..........dName: " + device.getDeviceName());     System.out.println(".......tow.........vid: " + device.getVendorId() + "/t pid: " + device.getProductId());    }    System.out.println("........................after..................................");  }

結果我們都能看到有兩個設備

Android,Usb設備,監聽,外設端口

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 信宜市| 葫芦岛市| 天镇县| 秦皇岛市| 武川县| 临潭县| 宁陵县| 中山市| 焉耆| 海林市| 密云县| 依兰县| 安庆市| 五指山市| 佛冈县| 杨浦区| 永春县| 邵阳县| 汶上县| 甘肃省| 东乌珠穆沁旗| 岑溪市| 太原市| 繁昌县| 鄱阳县| 达州市| 唐海县| 肥西县| 固安县| 榆树市| 白银市| 高雄县| 巴林左旗| 霍山县| 台中市| 天门市| 桃园市| 香港| 鸡泽县| 揭东县| 万年县|