自定义SystemUI快捷按钮

例如要在系统下拉菜单栏中,增加自定义按钮,可以在\frameworks\base\packages\SystemUI\src\com\android\systemui\qs\tiles中自定义一个Tile来实现


image.png

/*

  • Copyright (C) 2014 The Android Open Source Project
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  •  http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.
    */

package com.android.systemui.qs.tiles;

import java.util.Collection;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.content.res.Resources;

import com.android.systemui.R;
import com.android.systemui.qs.QSDetailItems;
import com.android.systemui.qs.QSDetailItems.Item;
import com.android.systemui.qs.QSTile;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class VoxTile extends QSTile<QSTile.BooleanState> {

boolean enabled = false;
private static final String VOC_ACTION = "com.jepower.intent.action.VOX_STATE";

public VoxTile(Host host) {
    super(host);
}

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

@Override
protected BooleanState newTileState() {
    return new BooleanState();
}

@Override
public void setListening(boolean listening) {

}

@Override
protected void handleClick() {
      enabled = !enabled;
    if(mContext != null) {
         Intent intent = new Intent();
         intent.setAction(VOC_ACTION);
         intent.putExtra("state", enabled);
         mContext.sendBroadcast(intent);
         android.util.Log.e("Linjiazhi", "sendBroadcast " + VOC_ACTION + " " + enabled);
    }
    refreshState();
}

@Override
protected void handleUpdateState(BooleanState state, Object arg) {
      final Resources r = mContext.getResources();
    state.visible = true;
    
    if(enabled) {
        state.icon = ResourceIcon.get(R.drawable.ic_qs_vox_enabled);
        state.label = r.getString(R.string.quick_settings_vox_label);
    } else {
        state.icon = ResourceIcon.get(R.drawable.ic_qs_vox_disabled);
        state.label = r.getString(R.string.quick_settings_vox_label);
    }

}

}
然后在\frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\QSTileHost中添加初始化


image.png

这里会去读取res/values/config.xml


image.png

将在QSTileHost中定义的vox配置在这里即可
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容