android usb弹窗权限r,Android USB权限对话框永远不会出现
我寫了一個簡單的應用程序,通過USB將命令發送到連接到Android 4.0平板電腦的USB打印機.出于某種原因,我無法獲得聲明接口和打開連接的權限.這是相關的代碼:
public class TestPrintActivity extends Activity {
private UsbManager mUsbManager;
private UsbDevice mDevice;
private UsbDeviceConnection mConnection;
private UsbInterface mInterface;
private UsbEndpoint mBulkIn;
private UsbEndpoint mBulkOut;
private static final String ACTION_USB_PERMISSION =
"com.demo.xprinter.USB_PERMISSION";
private PendingIntent mPermissionIntent;
private BroadcastReceiver mUsbReceiver;
@Override
private static final String ACTION_USB_PERMISSION =
"com.demo.printerDemo.USB_PERMISSION";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_print);
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);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
registerReceiver(mUsbReceiver, filter);
mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(device != null){
//call method to set up device communication
openPort(device);
}
}
else {
Toast.makeText(getApplicationContext(), "Denied!", Toast.LENGTH_SHORT).show();
}
}
}
}
};
}
public void onResume() {
super.onResume();
HashMap deviceList = mUsbManager.getDeviceList();
for (HashMap.Entry entry : deviceList.entrySet()) {
UsbDevice aDevice = entry.getValue();
if ((aDevice.getProductId() == 8965) && (aDevice.getVendorId() == 1659) ){
if (mUsbManager.hasPermission(aDevice))
openPort(aDevice);
else
mUsbManager.requestPermission(aDevice, mPermissionIntent);
break;
}
}
}
這是清單的相關部分:
package="com.demo.printerDemo"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="14"
android:targetSdkVersion="17" />
我已經配置了設備,以便在連接打印機時啟動我的應用程序,并在枚舉期間找到設備(onResume())并調用權限請求.但是,無論出于何種原因,我從未看到“請求權限”對話框(我在網上看過它的截圖),也沒有看到onReceive().任何想法為什么會這樣?
解決方法:
1)為什么你有兩個成員ACTION_USB_PERMISSION聲明?
2)在onCreate()中嘗試像這樣注冊你的Intent,也許有區別:
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(ACTION_USB_PERMISSION);
context.registerReceiver(usbReceiver, filter);
3)由于Android中的錯誤(http://code.google.com/p/android/issues/detail?id=25703),我不確定是否曾調用過ACTION_USB_ACCESSORY_ATTACHED操作.嘗試在AndroidManifest.xml中添加以下內容:
標簽:host,android,printing,usb
來源: https://codeday.me/bug/20191008/1874180.html
總結
以上是生活随笔為你收集整理的android usb弹窗权限r,Android USB权限对话框永远不会出现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android webview rem,
- 下一篇: android 使用javascript