Android 通過廣播監聽USB連接狀態的改變

      網友投稿 2022 2022-05-28

      一、原理講解

      Android通過廣播監聽USB連接狀態的改變的動作在UsbManager.java文件里,為ACTION_USB_STATE。然而在UsbManager中,該常量的注釋中包含{@hide},該注釋是控制該API是否開放用的。未開發的API意味著google可以隨時調整該API的定義或實現方式,而不用保證向下兼容。

      所以盡量避免調用未開發的API,因為一旦系統升級導致其實現方式改變,程序很有可能就直接崩潰了。

      注意:

      目前仍未發現 ACTION_USB_ACCESSORY_XXX 這種廣播

      插拔手機與電腦之間的USB線:ACTION_USB_STATE(主要參數是 Boolean connected)

      插拔U盤發送的是OTG廣播:ACTION_USB_DEVICE_XXX,XXX代表了ATTACH 或 DETACH,這種情況是手機是主,u盤是從,手機給U盤供電。

      二、代碼如下

      1、MyUsbManager.java

      package com.example.testapp; public class MyUsbManager { /** * Broadcast Action: A sticky broadcast for USB state change events when in device mode. * This is a sticky broadcast for clients that includes USB connected/disconnected state, *

        *
      • {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected. *
      • {@link #USB_CONFIGURED} boolean indicating whether USB is configured. currently zero if not configured, one for configured. *
      • {@link #USB_FUNCTION_ADB} boolean extra indicating whether the adb function is enabled *
      • {@link #USB_FUNCTION_RNDIS} boolean extra indicating whether the RNDIS ethernet function is enabled *
      • {@link #USB_FUNCTION_MTP} boolean extra indicating whether the MTP function is enabled *
      • {@link #USB_FUNCTION_PTP} boolean extra indicating whether the PTP function is enabled *
      • {@link #USB_FUNCTION_PTP} boolean extra indicating whether the accessory function is enabled *
      • {@link #USB_FUNCTION_AUDIO_SOURCE} boolean extra indicating whether the audio source function is enabled *
      • {@link #USB_FUNCTION_MIDI} boolean extra indicating whether the MIDI function is enabled *
      */ public static final String ACTION_USB_STATE = "android.hardware.usb.action.USB_STATE"; /** * Boolean extra indicating whether USB is connected or disconnected. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. */ public static final String USB_CONNECTED = "connected"; /** * Boolean extra indicating whether USB is configured. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. */ public static final String USB_CONFIGURED = "configured"; /** * Boolean extra indicating whether confidential user data, such as photos, should be * made available on the USB connection. This variable will only be set when the user * has explicitly asked for this data to be unlocked. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. */ public static final String USB_DATA_UNLOCKED = "unlocked"; /** * A placeholder indicating that no USB function is being specified. * Used to distinguish between selecting no function vs. the default function in {@link #setCurrentFunction(String)}. */ public static final String USB_FUNCTION_NONE = "none"; /** * Name of the adb USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast */ public static final String USB_FUNCTION_ADB = "adb"; /** * Name of the RNDIS ethernet USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast */ public static final String USB_FUNCTION_RNDIS = "rndis"; /** * Name of the MTP USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast */ public static final String USB_FUNCTION_MTP = "mtp"; /** * Name of the PTP USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast */ public static final String USB_FUNCTION_PTP = "ptp"; /** * Name of the audio source USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast */ public static final String USB_FUNCTION_AUDIO_SOURCE = "audio_source"; /** * Name of the MIDI USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast */ public static final String USB_FUNCTION_MIDI = "midi"; }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      Android 通過廣播監聽USB連接狀態的改變

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      68

      69

      70

      71

      72

      73

      74

      75

      76

      77

      78

      79

      80

      81

      82

      83

      84

      2、UsbActivity.java

      package com.example.testapp; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.hardware.usb.UsbAccessory; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbManager; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; public class UsbActivity extends Activity { private TextView textView; private UsbManager usbManager; private BroadcastReceiver receiver; private IntentFilter intentFilter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ScrollView scrollView = new ScrollView(this); LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); textView = new TextView(this); scrollView.addView(linearLayout); linearLayout.addView(textView); setContentView(scrollView); usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); addText("action : " + action); if(intent.hasExtra(UsbManager.EXTRA_PERMISSION_GRANTED)) { boolean permissionGranted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false); addText("permissionGranted : " + permissionGranted); } switch(action) { case UsbManager.ACTION_USB_ACCESSORY_ATTACHED: case UsbManager.ACTION_USB_ACCESSORY_DETACHED: //Name of extra for ACTION_USB_ACCESSORY_ATTACHED and ACTION_USB_ACCESSORY_DETACHED broadcasts containing the UsbAccessory object for the accessory. UsbAccessory accessory = intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); addText(accessory.toString()); break; case UsbManager.ACTION_USB_DEVICE_ATTACHED: case UsbManager.ACTION_USB_DEVICE_DETACHED: //Name of extra for ACTION_USB_DEVICE_ATTACHED and ACTION_USB_DEVICE_DETACHED broadcasts containing the UsbDevice object for the device. UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); addText(device.toString()); break; case MyUsbManager.ACTION_USB_STATE: /* *

    1. {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected. *
    2. {@link #USB_CONFIGURED} boolean indicating whether USB is configured. * currently zero if not configured, one for configured. *
    3. {@link #USB_FUNCTION_ADB} boolean extra indicating whether the * adb function is enabled *
    4. {@link #USB_FUNCTION_RNDIS} boolean extra indicating whether the * RNDIS ethernet function is enabled *
    5. {@link #USB_FUNCTION_MTP} boolean extra indicating whether the * MTP function is enabled *
    6. {@link #USB_FUNCTION_PTP} boolean extra indicating whether the * PTP function is enabled *
    7. {@link #USB_FUNCTION_PTP} boolean extra indicating whether the * accessory function is enabled *
    8. {@link #USB_FUNCTION_AUDIO_SOURCE} boolean extra indicating whether the * audio source function is enabled *
    9. {@link #USB_FUNCTION_MIDI} boolean extra indicating whether the * MIDI function is enabled * */ boolean connected = intent.getBooleanExtra(MyUsbManager.USB_CONNECTED, false); addText("connected : " + connected); boolean configured = intent.getBooleanExtra(MyUsbManager.USB_CONFIGURED, false); addText("configured : " + configured); boolean function_adb = intent.getBooleanExtra(MyUsbManager.USB_FUNCTION_ADB, false); addText("function_adb : " + function_adb); boolean function_rndis = intent.getBooleanExtra(MyUsbManager.USB_FUNCTION_RNDIS, false); addText("function_rndis : " + function_rndis); boolean function_mtp = intent.getBooleanExtra(MyUsbManager.USB_FUNCTION_MTP, false); addText("function_mtp : " + function_mtp); boolean function_ptp = intent.getBooleanExtra(MyUsbManager.USB_FUNCTION_PTP, false); addText("usb_function_ptp : " + function_ptp); boolean function_audio_source = intent.getBooleanExtra(MyUsbManager.USB_FUNCTION_AUDIO_SOURCE, false); addText("function_audio_source : " + function_audio_source); boolean function_midi = intent.getBooleanExtra(MyUsbManager.USB_FUNCTION_MIDI, false); addText("function_midi : " + function_midi); break; } } }; intentFilter = new IntentFilter(); intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED); intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED); intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); intentFilter.addAction(MyUsbManager.ACTION_USB_STATE); registerReceiver(receiver, intentFilter); boolean hasUsbHost = getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_HOST); boolean hasUsbAccessory = getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY); addText("hasUsbHost : " + hasUsbHost); addText("hasUsbAccessory : " + hasUsbAccessory); } @Override protected void onDestroy() { unregisterReceiver(receiver); super.onDestroy(); } private void addText(String str) { textView.setText(textView.getText().toString() + str + "\n"); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      68

      69

      70

      71

      72

      73

      74

      75

      76

      77

      78

      79

      80

      81

      82

      83

      84

      85

      86

      87

      88

      89

      90

      91

      92

      93

      94

      95

      96

      97

      98

      99

      100

      101

      102

      103

      104

      105

      106

      107

      108

      109

      110

      111

      112

      113

      114

      115

      116

      117

      118

      119

      120

      121

      122

      123

      124

      三、運行截圖

      四、ACTION_USB_DEVICE_ATTACHED 和 ACTION_USB_DEVICE_DETACHED的缺點

      廣播是去監測U盤插入和拔出的,也就意味著,你只要一插入或者一拔出U盤,就是收到這兩個廣播。它不會管你的設備有沒有準備好,有沒有mounted或者unmounted。

      因此就需要引入一個新的廣播 android.os.storage.extra.VOLUME_STATE。

      這個廣播就是用來監聽Volume狀態的。當外置Usb設備在Mounted或者UnMounted的時候則就可以用來做監聽 int 類型的 VolumeInfo.EXTRA_VOLUME_STATE。

      注意:android.os.storage.extra.VOLUME_STATE 一定要聲明讀寫權限,否則是收不到的。

      1

      2

      Android API

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

    10. 上一篇:webpack5熱更新打包TS
      下一篇:C++奧賽一本通遞推題解
      相關文章
      久久精品熟女亚洲av麻豆| 亚洲AV无码国产精品永久一区| 国产亚洲成在线播放va| 亚洲一区二区三区高清不卡| 亚洲乱码中文字幕小综合| 中文字幕亚洲第一在线| 亚洲免费精彩视频在线观看| 亚洲AV成人片色在线观看| 久久久亚洲精品无码| 亚洲卡一卡2卡三卡4卡无卡三| 亚洲成色WWW久久网站| 人人狠狠综合久久亚洲婷婷| 亚洲国产精品一区二区第一页| 亚洲人成网77777亚洲色| 亚洲一区二区女搞男| 亚洲女初尝黑人巨高清| 亚洲国产精品成人久久| 亚洲AV综合色一区二区三区| 亚洲av鲁丝一区二区三区| 亚洲另类激情综合偷自拍| 亚洲激情校园春色| 91丁香亚洲综合社区| 亚洲精品又粗又大又爽A片| 亚洲国产成人精品无码区花野真一| 亚洲欧美在线x视频| 亚洲精品岛国片在线观看| 国产成人精品亚洲精品| 亚洲免费人成在线视频观看| 亚洲91av视频| 亚洲一区二区三区四区视频 | 亚洲综合久久夜AV | 久久久久久亚洲精品不卡| 亚洲人成中文字幕在线观看| 亚洲va久久久噜噜噜久久狠狠 | 亚洲av无码成h人动漫无遮挡| 亚洲图片在线观看| 亚洲中文字幕无码一去台湾 | 亚洲av无码乱码国产精品fc2| 久久亚洲精品成人av无码网站| 亚洲国产成人91精品| 亚洲国产成人精品无码区花野真一 |