自学内容网 自学内容网

高通Android 12/13实现USB拔出关机功能

思路流程

1、监听广播->接受USB断开或者USB不充电广播->执行关机逻辑

涉及类 UsbManager/UsbDeviceManager

\frameworks\base\services\usb\java\com\android\server\usb\UsbDeviceManager.java

\frameworks\base\services\com\android\hardware\usb\UsbManager.java

2、自定义系统服务Service接收Intent.ACTION_POWER_DISCONNECTED广播代码逻辑

mContentResolver = mContext.getContentResolver();
/**mUsbReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
    Log.e("ZM","Receiver USBMode SHUTDOWN BroadcastReceiver Action="+intent.getAction());
                if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction()) || UsbManager.ACTION_USB_DEVICE_DETACHED.equals(intent.getAction())) {
Log.e("ZM","Receiver USBMode SHUTDOWN BroadcastReceiver");
                    handleUsbDisconnected();
                }
            }
        };**/


        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        mContext.registerReceiver(mUsbReceiver, filter);

3、 handleUsbDisconnected()具体实现逻辑如下所示

private void handleUsbDisconnected() {
        int usbPlugShutdown = Settings.Global.getInt(mContentResolver, USB_PLUG_SHUTDOWN_KEY, 0);
Log.e("handleUsbDisconnected","usbplugShutdown="+usbPlugShutdown);
if(usbPlugShutdown == 1 ){
   Log.e("handleUsbDisconnected","device shutdown");
   shutdown();   
}
    }

4、设置USB拔出关机模式 代码如下图所示

  /**
     * 设置USB模式
     *
     * @param mode
     */
    public void setUSBMode(int mode) {
//isSettingUsbMode = true; // 设置完成后将标志恢复为 false
        UsbManager mUsbManager = (UsbManager) mContext.getSystemService(UsbManager.class);
if(mUsbManager != null){
   Log.e("ZM","=========setUSBMode============="+mode);
           mUsbManager.setCurrentFunctions((long) mode);
   // Log.e("tian getUSBMode::::::::::" + String.valueOf(mUsbManager.getCurrentFunctions()));
   //isSettingUsbMode = false; // 设置完成后将标志恢复为 false
}

    }

5、获取USB拔出关机模式状态 代码如下图所示


    /**
     * 获取USB模式
     *
     * @return
     */
    public int getUSBMode() {
        UsbManager mUsbManager = (UsbManager) mContext.getSystemService(UsbManager.class);
if(mUsbManager != null){
Log.e("ZM","=========getUSBMode============="+(int) mUsbManager.getCurrentFunctions());
        return (int) mUsbManager.getCurrentFunctions();
}
return 0;
    }

6、设置USB拔出关机和不关机adb指令 

adb shell settings put global usb_plug_shutdown 0 //设置USB拔出不关机

db shell settings put global usb_plug_shutdown  1 //设置USB拔出关机


adb shell settings get global usb_plug_shutdown 获取USB拔出状态值

7、感兴趣可以去了解USB模式 UsbDeviceManager 打印 mCurrentFunctions值

  protected void updateUsbStateBroadcastIfNeeded(long functions) {
            // send a sticky broadcast containing current USB state
            Intent intent = new Intent(UsbManager.ACTION_USB_STATE);
            intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
                    | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND
                    | Intent.FLAG_RECEIVER_FOREGROUND);
            intent.putExtra(UsbManager.USB_CONNECTED, mConnected);
//zm fix
Slog.d(TAG, "ZM skip updateUsbStateBroadcastIfNeeded " + mConnected +"mCurrentFunctions="+(int)mCurrentFunctions);
            intent.putExtra(UsbManager.USB_HOST_CONNECTED, mHostConnected);
            intent.putExtra(UsbManager.USB_CONFIGURED, mConfigured);
            intent.putExtra(UsbManager.USB_DATA_UNLOCKED,
                    isUsbTransferAllowed() && isUsbDataTransferActive(mCurrentFunctions));

            long remainingFunctions = functions;
            while (remainingFunctions != 0) {
                intent.putExtra(UsbManager.usbFunctionsToString(
                        Long.highestOneBit(remainingFunctions)), true);
                remainingFunctions -= Long.highestOneBit(remainingFunctions);
            }

            // send broadcast intent only if the USB state has changed
            if (!isUsbStateChanged(intent)) {
                if (DEBUG) {
                    Slog.d(TAG, "skip broadcasting " + intent + " extras: " + intent.getExtras());
                }
                return;
            }

            if (DEBUG) Slog.d(TAG, "broadcasting " + intent + " extras: " + intent.getExtras());
            sendStickyBroadcast(intent);
            mBroadcastedIntent = intent;
        }

8、到这里结束了,转载请注明出处高通Android 12/13实现USB拔出关机功能-CSDN博客,谢谢!


原文地址:https://blog.csdn.net/qq_15950325/article/details/139356895

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!