自定义SystemUI快捷按钮

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


自定义SystemUI快捷按钮_第1张图片
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 {

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中添加初始化


自定义SystemUI快捷按钮_第2张图片
image.png

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


image.png

将在QSTileHost中定义的vox配置在这里即可

你可能感兴趣的:(自定义SystemUI快捷按钮)