Android 增加虚拟的关机按钮实现虚拟按键关机功能

  • 参考链接:

    https://blog.csdn.net/ansondroider/article/details/97259148

  • 准备一个关机的图标,网上去找

    • 将这个图标放入 Android 源码目录,如下
    frameworks/base/packages/SystemUI/res/drawable-nodpi/ic_sysbar_power.png
    
  • 功能实现

    • 在下面的文件里面增加相关的字符:
    • frameworks/base/packages/SystemUI/res/values/strings.xml
      倒数第二行添加:
     <string name="accessibility_power" translatable="false">Power</string>
    
    • 添加按键布局:
    • frameworks/base/packages/SystemUI/res/layout/power.xml
    <com.android.systemui.statusbar.policy.KeyButtonView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res-auto"
    android:id="@+id/power"
    android:layout_width="@dimen/navigation_key_width"
    android:layout_height="match_parent"
    android:layout_weight="0"
    android:src="@drawable/ic_sysbar_power"
    systemui:keyCode="0"
    android:scaleType="center"
    android:contentDescription="@string/accessibility_power"
    android:paddingStart="@dimen/navigation_key_padding"
    android:paddingEnd="@dimen/navigation_key_padding"
    />
    
    • 在这个文件里面添加如下内容
    • frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarInflaterView.java
    // 添加类的属性
    54     public static final String POWER = "power";
    // 在 inflateButton 方法里面 添加大致在 这个地方的内容
    287         // Add by Frey_chen 20200330
    288         // Add Power layout button ---
    289         }else if (POWER.equals(button)) {
    290             v = inflater.inflate(R.layout.power, parent, false);
    291             if (landscape && isSw600Dp()) {
    292                 setupLandButton(v);
    293             }
    294         // ---
    
    • 修改 Android 默认配置
    • frameworks/base/packages/SystemUI/res/values-sw600dp/config.xml
    <string name="config_navBarLayout" translatable="false">space;volume_sub,back,home,recent,volume_add,screenshot,power;menu_ime</string>
    
    • frameworks/base/packages/SystemUI/res/values-sw900dp/config.xml
    <string name="config_navBarLayout" translatable="false">space;volume_sub,back,home,recent,volume_add,screenshot,power;menu_ime</string>
    
    • frameworks/base/packages/SystemUI/res/values/config.xml
    <string name="config_navBarLayout" translatable="false">space;volume_sub,back,home,recent,volume_add,screenshot,power;menu_ime</string>
    
    • 设置按键可见
    • frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
    1443     private View.OnClickListener mPowerClickListener = new View.OnClickList     ener(){
    1444         public void onClick(View v){
    1445             Intent intent = new Intent("android.intent.action.POWER_MENU");
    1446             mContext.sendBroadcast(intent);
    1447         }
    1448     };
    
    1478         // Add by Frey_chen 20200330
    1479         // Add power button function ---
    1480         ButtonDispatcher powerButton=mNavigationBarView.getPowerButton();
    1481         powerButton.setOnClickListener(mPowerClickListener);
    1482         //powerButton.setOnTouchListener(mPowerTouchListener);
    1483         powerButton.setVisibility(View.VISIBLE);
    1484         // ---
    
    • frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
    215         mButtonDisatchers.put(R.id.screenshot, new ButtonDispatcher(R.id.scr    eenshot));
    216         mButtonDisatchers.put(R.id.power, new ButtonDispatcher(R.id.power));
    217         mButtonDisatchers.put(R.id.volume_add, new ButtonDispatcher(R.id.vol    ume_add));
    
    • 设置广播监听
    • frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
    1886         // Add by Frey_chen
    1887         // Add monitor for POWER_MENU
    1888         IntentFilter ifPower = new IntentFilter("android.intent.action.POWE     R_MENU");
    1889         context.registerReceiver(new BroadcastReceiver(){
    1890             @Override
    1891             public void onReceive(Context context, Intent intent) {
    1892                 //show global actions dialog
    1893                 showGlobalActionsInternal();
    1894             }
    1895         }, ifPower);
    
posted @ 2020-03-30 14:20  陈富林  阅读(1518)  评论(1编辑  收藏  举报