参考文章
https://blog.youkuaiyun.com/h1217256980/article/details/105726771
补充
按照文章中的可以实现效果,本文针对第一点自定义类做一些细化。
新增按钮其实比较简单,因为原生框架已经搭建好了,我们只要集成实现关键方法就可以了。
QS添加按钮主要注意以下己点:
1、按钮状态(打开、关闭、不可用)
2、点击事件
3、长按事件
4、状态更新
adb获取当前qsSetting列表配置
adb shell settings get secure sysui_qs_tiles
西瓜桶
------因为这一天同事请我喝了一杯西瓜桶奶茶,刚好我在做添加新按钮,所以这个demo就叫这个名字了~!哈哈哈
demo代码仅供参考,因为Android大版本不同,构造方法和实现父类方法可能有一些区别。
package com.android.systemui.qs.tiles;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioManager;
import android.service.quicksettings.Tile;
import com.android.systemui.plugins.qs.QSTile;
import com.android.systemui.qs.QSHost;
import com.android.systemui.qs.tileimpl.QSTileImpl;
import javax.inject.Inject;
public class XiGuaTong extends QSTileImpl<QSTile.BooleanState> {
int index = 0;
int[] color = {Color.BLUE,Color.DKGRAY,Color.YELLOW};
@Inject
protected XiGuaTong(QSHost host) {
super(host);
}
@Override
public BooleanState newTileState() {
BooleanState state = new BooleanState();
state.label = this.getClass().getSimpleName();
state.handlesLongClick = false;
return state;
}
@Override
protected void handleClick() {
index++;
refreshState();
}
@Override
protected void handleUpdateState(BooleanState state, Object arg) {
state.state = index%4 == 3? Tile.STATE_INACTIVE: Tile.STATE_ACTIVE;
state.secondaryLabel = String.valueOf(index);
state.label = this.getClass().getSimpleName();
switch (index%4) {
case 0:
state.icon = new DrawableIcon(mContext.getDrawable(com.android.systemui.R.drawable.ic_volume_ringer));
break;
case 1:
state.icon = new DrawableIcon(mContext.getDrawable(com.android.systemui.R.drawable.ic_volume_ringer_vibrate));
break;
case 2:
state.icon = new DrawableIcon(mContext.getDrawable(com.android.systemui.R.drawable.ic_volume_ringer_mute));
break;
case 3:
break;
default:
break;
}
// int color = this.color[index%3];
}
@Override
public int getMetricsCategory() {
return 0;
}
@Override
public Intent getLongClickIntent() {
//
return null;
}
@Override
public CharSequence getTileLabel() {
return this.getClass().getSimpleName();
}
}
疑问
我本来想自己控制按钮的颜色,比如点击按钮,按钮背景色改变成想要的颜色,但是没找到。。。。