0

所以逻辑是:

  • 用户将 USB 连接到 Android 设备。
  • 用户按下“查找更新”按钮。
  • 该应用程序连接到 USB 并在主文件夹中找到任何 .apk 文件。
  • 应用程序会打开 USB 中的 .apk 文件。
  • .apk 运行并更新当前应用程序。

到目前为止我所拥有的:

findUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean foundUsb = false;
            usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
            deviceList = usbManager.getDeviceList();
            deviceIterator = deviceList.values().iterator();
            Log.d("UPDATER", "Pressed");
            while(deviceIterator.hasNext()){

                UsbDevice device = deviceIterator.next();
                Log.d("UPDATER", "Device Attached: " + device.getManufacturerName());

                if (!Objects.equals(device.getManufacturerName(), gv.getManufacturerIDPlaca())) {
                    Log.d("UPDATER", device.getManufacturerName());
                    foundUsb = true;
                    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(SetupActivity.this, 0, new Intent(ACTION_USB_PERMISSION), 0);

                    if (!usbManager.hasPermission(device)) {
                        Log.d("UPDATER", "No access");
                        usbManager.requestPermission(device, mPermissionIntent);
                        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
                    }

                    Log.d("UPDATER", "Has access");

                    usbDevice = device;
                    usbInterface = usbDevice.getInterface(0);
                    usbEndpoint = usbInterface.getEndpoint(0);
                    usbDeviceConnection = usbManager.openDevice(usbDevice);
                    packetSize = usbEndpoint.getMaxPacketSize();

                    new Thread(new Runnable() {
                        public void run() {
                            final byte[] buffer = new byte[packetSize];
                            usbDeviceConnection.claimInterface(usbInterface, forceClaim);
                            usbDeviceConnection.bulkTransfer(usbEndpoint, buffer, packetSize, TIMEOUT);
                            Log.d("UPDATER", String.valueOf(buffer));
                        }
                    }).start();
                }
            }

            if (!foundUsb) {
                Toast.makeText(context, "No USB detected", Toast.LENGTH_SHORT).show();
            }
        }
    });

这段代码在控制台中返回给我的是一个转换为字符串的缓冲区(字节)。

我想知道如何找到一个文件,比如说 app.apk 并打开它以实际更新当前应用程序(按下按钮的位置)。

4

1 回答 1

0

我终于想出了一个解决方案:

findUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean foundUsb = false;
            usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
            deviceList = usbManager.getDeviceList();
            deviceIterator = deviceList.values().iterator();
            while(deviceIterator.hasNext()){

                UsbDevice device = deviceIterator.next();
                Log.d("UPDATER", "Device Attached: " + device.getManufacturerName());

                if (!Objects.equals(device.getManufacturerName(), gv.getManufacturerIDPlaca())) {
                    foundUsb = true;
                    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(SetupActivity.this, 0, new Intent(ACTION_USB_PERMISSION), 0);

                    if (!usbManager.hasPermission(device)) {
                        usbManager.requestPermission(device, mPermissionIntent);
                        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
                    }

                    try {
                        File file = new File("/storage/usbhost/update-binary.apk");
                        if (file.exists()) {
                            Intent newIntent = new Intent(Intent.ACTION_VIEW);
                            newIntent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(newIntent);
                        }
                        else {
                            Toast.makeText(getApplicationContext(), R.string.UPDATE_APK_NO_ENCONTRADA, Toast.LENGTH_LONG).show();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
                    }
                }
            }

            if (!foundUsb) {
                Toast.makeText(context, R.string.USB_NO_CONECTADO, Toast.LENGTH_SHORT).show();
            }
        }
    });
于 2018-01-04T14:31:15.173 回答