概述
通过BroadcastReceiver广播,监听U盘设备插拔,但是鼠标,键盘设备监听不到,需要用到InputManager。
package com.intel.factorytest.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.intel.factorytest.R;
import java.util.HashMap;
import java.util.Map;
import android.hardware.input.InputManager;
import android.view.InputDevice;
import android.os.Handler;
import android.os.Message;
public class OTGTestActivity extends BaseActivity implements InputManager.InputDeviceListener{
private static final String TAG = "OTGTestActivity";
private TextView mcTestText;
private Button mSucBtn;
private UsbManager mUsbManager;
InputManager inputManager ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate()");
setContentView(R.layout.activity_otgtest);
super.initView();
mcTestText = findViewById(R.id.otg_text);
mSucBtn = findViewById(R.id.success_button);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume()");
IntentFilter filter = new IntentFilter();
filter.addAction("android.hardware.usb.action.USB_DEVICE_ATTACHED");
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
filter.addAction("android.hardware.usb.action.USB_STATE");
registerReceiver(mUsbStateChangeReceiver, filter);
if(inputManager == null){
inputManager = (InputManager) getSystemService(Context.INPUT_SERVICE);
}
inputManager.registerInputDeviceListener(this,null);
update_ui();
}
private void update_ui(){
if(checkOtgDevices()){
mcTestText.setText(getString(R.string.usb_status_host));
mSucBtn.setVisibility(View.VISIBLE);
}else{
mcTestText.setText(getString(R.string.insert_otg_cable));
mSucBtn.setVisibility(View.INVISIBLE);
}
}
private boolean checkOtgDevices(){
UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
if(mUsbManager.getDeviceList().size() > 0){
return true;
}
final int[] input_devices = InputDevice.getDeviceIds();
for (int i = 0; i < input_devices.length; i++) {
InputDevice device = InputDevice.getDevice(input_devices[i]);
if (device != null && !device.isVirtual() && device.isExternal()) {
return true;
}
}
return false;
}
private BroadcastReceiver mUsbStateChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: " + intent.getAction());
update_ui();
}
};
@Override
protected void onPause() {
Log.e(TAG, "onPause");
unregisterReceiver(mUsbStateChangeReceiver);
inputManager.unregisterInputDeviceListener(this);
super.onPause();
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
}
@Override
public void finish() {
setTestResult();
super.finish();
}
@Override
public void onInputDeviceAdded(int deviceId) {
Log.d(TAG, "onInputDeviceAdded deviceId = " + deviceId +"");
update_ui();
}
@Override
public void onInputDeviceRemoved(int deviceId) {
update_ui();
}
@Override
public void onInputDeviceChanged(int deviceId) {
update_ui();
}
}
最后
以上就是健忘发箍为你收集整理的安卓12,手机监听OTG设备插拔的全部内容,希望文章能够帮你解决安卓12,手机监听OTG设备插拔所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复