gpt4 book ai didi

android - 删除 USB 附件权限对话框

转载 作者:行者123 更新时间:2023-11-29 01:44:46 25 4
gpt4 key购买 nike

我正在开发一个使用 USB 配件的应用程序。当我将设备连接到 arduino 时,它会请求 usb 权限,并且在“默认使用”对话框中有复选框。当我选中并按确定时,它应该保存权限并执行不再询问我,但每次我连接 USB 时它都在请求许可。我想保存许可,有什么办法可以保存这个吗?

我的代码看起来像

@Override
public void onResume() {
super.onResume();

if (mInputStream != null && mOutputStream != null) {
// streams were not null");
return;
}
// streams were null");
UsbAccessory[] accessories = mUsbManager.getAccessoryList();
UsbAccessory accessory = (accessories == null ? null : accessories[0]);
if (accessory != null) {
if (mUsbManager.hasPermission(accessory)) {
openAccessory(accessory);
} else {
synchronized (mUsbReceiver) {
if (!mPermissionRequestPending) {
mUsbManager.requestPermission(accessory,
mPermissionIntent);
mPermissionRequestPending = true;
}
}
}
}

Android list 文件看起来

 <activity
android:name="pkg.sarvajal.soochakdroid.Main"
android:label="@string/Home"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>


<meta-data
android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="@xml/accessory_filter" />
</activity>

附件过滤器就像

<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-accessory manufacturer="Manufacturer" model="Model" version="1.0" />
<usb-device vendor-id="0x0403" product-id="0x0403" />
</resources>

我的 BroadcastReceiver 代码

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
//UsbAccessory accessory = UsbManager.getAccessory(intent);
UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
if (intent.getBooleanExtra(
UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
Flag_USB_Attached = true;
Flag_Count_USB_Attached = 0;
Flag_Count_USB_Restart_App = 0;
openAccessory(accessory);
String strdatetime =responce_date();
//sendSMS_background(phoneNumber, "USB Cable Connected at "+strdatetime);


} else {
// USB permission denied
}
}
} else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
//UsbAccessory accessory = UsbManager.getAccessory(intent);
UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
if (accessory != null && accessory.equals(mAccessory)) {
// accessory detached
Flag_USB_Attached = false;
Flag_Count_USB_Attached = 0;
closeAccessory();
String strdatetime =responce_date();
//sendSMS_background(phoneNumber, "Communication Stopped! \n USB Cable Disconnected at "+strdatetime);
//SMS_USB();

}
}
}

和设置附件代码

/ Setting up accessory
private void setupAccessory() {
//mUsbManager = UsbManager.getInstance(this);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
registerReceiver(mUsbReceiver, filter);
if (getLastNonConfigurationInstance() != null) {
mAccessory = (UsbAccessory) getLastNonConfigurationInstance();
openAccessory(mAccessory);
}
}
// Opening Accessory Mode
private void openAccessory(UsbAccessory accessory) {
mFileDescriptor = mUsbManager.openAccessory(accessory);
if (mFileDescriptor != null) {
mAccessory = accessory;
FileDescriptor fd = mFileDescriptor.getFileDescriptor();
mInputStream = new FileInputStream(fd);
mOutputStream = new FileOutputStream(fd);
Thread thread = new Thread(null, this, "OpenAccessoryTest");
thread.start();
// Accessory opened
} else {
// failed to open accessory
}
}
// close Accessory
private void closeAccessory() {
try {
// T.cancel();
if (mFileDescriptor != null) {
mFileDescriptor.close();
}
} catch (IOException e) {
} finally {
mFileDescriptor = null;
mAccessory = null;
}
}

最佳答案

根据Android USB host documentation ,您的应用程序有权访问设备,直到设备断开连接。 This question提供了一种抑制权限对话框的解决方案。在您的 list 文件中创建一个 Intent 过滤器,并在 xml 文件中提供设备信息。这将启用使用广播接收器枚举设备。

另一个引用是 this question .后者适用于 Root设备,我还没有亲自尝试过。

编辑:

list 文件:我在 list 文件中注册了我的广播接收器。

<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</receiver>

为了方便起见,我将广播接收器放在一个单独的类中:

public class PmrtReceiver extends BroadcastReceiver {


public void onReceive(Context context, Intent intent) {

String action = intent.getAction();
if(UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)){

Toast.makeText(context, "Device Detected",Toast.LENGTH_LONG).show();


} else if(UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)){

Toast.makeText(context, "Device Detached",Toast.LENGTH_LONG).show();

}

}

};

我不需要做任何其他事情来抑制权限对话框。

查看 Android 示例中的 USB 导弹发射器示例。您的设备过滤器应该是这样的:

<?xml version="1.0" encoding="utf-8"?>

<resources>
<usb-device vendor-id="1234" product-id="5678" />
</resources>

您有十六进制数形式的 VID 和 PID。

关于android - 删除 USB 附件权限对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22188856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com