Android在长按关机键弹框中添加新的选项(如截屏,提示语)

首先从PhoneWindowsManager.java看起,这里会调用

private void interceptPowerKeyDown(boolean handled) {  

     mPowerKeyHandled = handled;  

    if (!handled) {  

       mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout()); 

     }  

 } 

从而进入mPowerLongPress,接着看mPowerLongPress,在这里调用了showGlobalActionsDialog()方法,

private final Runnable mPowerLongPress = new Runnable() {  
    @Override  
    public void run() {  
        // The context isn't read  
        if (mLongPressOnPowerBehavior < 0) {  
            mLongPressOnPowerBehavior = mContext.getResources().getInteger(  
                    com.android.internal.R.integer.config_longPressOnPowerBehavior);  
        }  
        ...  
  
        switch (resolvedBehavior) {  
        case LONG_PRESS_POWER_NOTHING:  
            break;  
        case LONG_PRESS_POWER_GLOBAL_ACTIONS:  
            mPowerKeyHandled = true;  
            if (!performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false)) {  
                performAuditoryFeedbackForAccessibilityIfNeed();  
            }  
            sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);  
            showGlobalActionsDialog();  
            break;  
            ...  
        }  
    }  
};  
showGlobalActionsDialog方法中又调用了mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned()); 

void showGlobalActionsDialog() {  
    if (mGlobalActions == null) {  
        mGlobalActions = new GlobalActions(mContext, mWindowManagerFuncs);  
    }  
    final boolean keyguardShowing = keyguardIsShowingTq();  
    mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned());  
    if (keyguardShowing) {  
        // since it took two seconds of long press to bring this up,  
        // poke the wake lock so they have some time to see the dialog.  
        mKeyguardMediator.userActivity();  
    }  
}  

这个方法,至此我们看到了长按关机键弹框的关键类GlobalActions.java,弹框中所有的数据都是在GlobalActionsDialog createDialog()这个方法被创建的,

GlobalActionsDialog方法可以看 mItems.add这个方法是添加菜单选项的,该菜单的添加的第一个选项就是关机选项。

以添加飞行模式为例 mItems.add(mAirplaneModeOn);

这里的mAirplaneModeOn是一个实现了Action的一个对象,我们可以仿照飞行模式添加一个提示语的选项(项目中的需求),

我们只需要定义一个类实现Action,class  PromptAction implements Action ,在create()方法中,绑定自己的布局,

private class PromptAction implements Action{
        public CharSequence getLabelForAccessibility(Context context) {
            return null;
        }

        @Override
        public View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
            View view=inflater.inflate(R.layout.promt_action_layout,parent,false);
            return view;
        }

        @Override
        public void onPress() {

        }

        @Override
        public boolean showDuringKeyguard() {
            return false;
        }

        @Override
        public boolean showBeforeProvisioning() {
            return false;
        }

        @Override
        public boolean isEnabled() {
            return false;
        }

    }

需要注意的是,实现Action的一个方法

@Override
public boolean showDuringKeyguard() {
return false;
}

这里的返回值决定此项是否会出现在锁屏界面的弹框里。之后我们在添加飞行模式的地方,加上如下代码:

else if(GLOBAL_ACTION_KEY_PROMPT.equals(actionKey)){
mItems.add(new PromptAction());
}

最后,在frameworks/base/core/res/res/values/config.xml中config_globalActionsList string-array中添加prompt

<string-array translatable="false" name="config_globalActionsList">
<item>power</item>
<item>reboot</item>
<item>airplane</item>
<item>screenshot</item>
<item>data_connection</item> 
<item>silent</item>
<item>bugreport</item>
<item>users</item>
<item>prompt</item>
</string-array>

这就实现了在长按关机弹框中添加提示语的功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值