最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2018年12月13日 11:54:08   作者:ChaoLi_Chen  
今天小編就為大家分享一篇關于Android Usb設備的監(jiān)聽(Dev)外設端口的判定以及耳機的插拔,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

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

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

我個人的建議就是將一部分代碼(根據(jù)個人情況而定)放到服務里面,或者是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);
 //注冊廣播 動態(tài)注冊
 registerReceiver(receiver, filter);
 }
 /**
 * 創(chuàng)建廣播的類
 */
 BroadcastReceiver receiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();
  //判斷外設
  if (action.equals(TAGIN)) {
  ToastUtils.shwotoast(context, "外設已經(jīng)連接");
  //Toast.makeText(context, "外設已經(jīng)連接", Toast.LENGTH_SHORT).show();
  }
  if (action.equals(TAGOUT)) {
  if (BOOLEAN) {
   ToastUtils.shwotoast(context, "外設已經(jīng)移除");
   //Toast.makeText(context, "外設已經(jīng)移除", Toast.LENGTH_SHORT).show();
  }
  }
  //判斷存儲usb
  if (action.equals(TAGUSB)) {
  boolean connected = intent.getExtras().getBoolean("connected");
  if (connected) {
   ToastUtils.shwotoast(context, "USB 已經(jīng)連接");
   //Toast.makeText(MainActivity.this, "USB 已經(jīng)連接",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開頭的是十六位的 然后轉化成十進制的數(shù)就能看到自己的東西了)

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..................................");
  }

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

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內(nèi)容請查看下面相關鏈接

相關文章

最新評論

榆中县| 汶上县| 璧山县| 平山县| 富川| 卢龙县| 鄂托克前旗| 舞阳县| 乐至县| 新安县| 东乌珠穆沁旗| 调兵山市| 松江区| 湟源县| 满城县| 安多县| 辽阳市| 红河县| 佛教| 茂名市| 乡宁县| 孟津县| 白朗县| 隆林| 吉木萨尔县| 建水县| 桂阳县| 沈丘县| 衡阳县| 六盘水市| 昌宁县| 武冈市| 山丹县| 富平县| 抚远县| 乌兰县| 麻江县| 叙永县| 东至县| 新昌县| 达孜县|