Android 应用添加Tile到SystemUI QuickSettings

news2025/4/19 10:36:02

安卓源码里有谷歌给的关于 Tile 的说明。
frameworks/base/packages/SystemUI/docs/qs-tiles.md

SystemUI QuickSettings 简称QS,指的是 下拉菜单里的区域。区域里的一个选项就是一个 Tile 。

下图是 frameworks/base/packages/SystemUI/docs/ 里的附图示例,
在这里插入图片描述

SystemUI 会加载自己的Tile ,也会加载第三方应用的 Tile 。

SystemUI 里有配置,会默认加载一些Tile到QS面板 ,如 wifi 、蓝牙的Tile 。未配置的Tile不会显示的QS面板里,但备选区域里有,用户点击 编辑 按钮,可以通过拖拽的方式添加、删除Tile。

SystemUI QuickSettings 的加载流程先按住不表。

先试试作为第三方应用,如何实现添加Tile到SystemUI。

1.创建Service继承TileService

package com.test.luodemo.tileservice;

import android.content.Intent;
import android.service.quicksettings.TileService;
import android.util.Log;

public class ClickTileService extends TileService {

    public static final String TAG = ClickTileService.class.getSimpleName();

    public ClickTileService() {
    }

    @Override
    public void onTileAdded() {
        super.onTileAdded();
        Log.d(TAG,"onTileAdded");
    }

    @Override
    public void onTileRemoved() {
        super.onTileRemoved();
        Log.d(TAG,"onTileRemoved");
    }

    @Override
    public void onStartListening() {
        super.onStartListening();
        Log.d(TAG,"onStartListening");
    }

    @Override
    public void onStopListening() {
        super.onStopListening();
        Log.d(TAG,"onStopListening");
    }

    @Override
    public void onClick() {
        super.onClick();
        Log.d(TAG,"onClick");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }
}

示例创建了 ClickTileService ,继承 android.service.quicksettings.TileService ,重写 TileService 的方法。

  • onTileAdded: Tile 被添加到 QS 面板时的回调。
  • onTileRemoved: Tile 从 QS 面板移除时的回调。
  • onStartListening: called when QS is opened and the tile is showing. This marks the start of the window when calling getQSTile is safe and will provide the correct object.
  • onStopListening: called when QS is closed or the tile is no longer visible by the user. This marks the end of the window described in onStartListening.
  • onClick: 点击事件。

观察原生的选项,点击事件主要有两种:

  • 做切换功能,作用是打开、关闭某个功能,如打开关闭wifi。
  • 做跳转功能,如点击打开应用的某个页面。

点击做切换功能

    @Override
    public void onClick() {
        super.onClick();
        Log.d(TAG,"onClick");
        Tile tile = getQsTile();
        int curState = tile.getState();
        if (curState == Tile.STATE_ACTIVE) {
            tile.setState(Tile.STATE_INACTIVE);
            tile.setStateDescription("StateDescriptionOff");
            tile.setSubtitle("SubtitleOff");
            tile.updateTile();
        } else if (curState == Tile.STATE_INACTIVE) {
            tile.setState(Tile.STATE_ACTIVE);
            tile.setStateDescription("StateDescriptionOn");
            tile.setSubtitle("SubtitleOn");
            tile.updateTile();
        }

    }

通过 getQsTile() 可以获取到 Tile 对象,就可以更新状态、描述。

本例 on/off 对比,
在这里插入图片描述

点击做跳转功能

    @Override
    public void onClick() {
        super.onClick();
        Log.d(TAG,"onClick");
        Intent intent = new Intent(ClickTileService.this, TileActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //startActivity(intent);//会打开页面,下拉菜单不会收起
        startActivityAndCollapse(intent);//会打开页面,下拉菜单收起
    }

跳转功能 ,一看便知。

  • startActivity :会打开页面,但是下拉菜单不会自动收起。手动收起下拉菜单后才能看到 TileActivity 。不友好,不推荐。
  • startActivityAndCollapse :TileService 里的方法,会打开页面,下拉菜单会自动收起,然后显示 TileActivity 。操作方式友好,推荐。

2.Service注册到AndroidManifest

前面写了 Service ,四大组件之一 ,注册到 AndroidManifest ,无需解释了。

TileService 的源码注释有说明。

A TileService provides the user a tile that can be added to Quick Settings. Quick Settings is a space provided that allows the user to change settings and take quick actions without leaving the context of their current app.
The lifecycle of a TileService is different from some other services in that it may be unbound during parts of its lifecycle. Any of the following lifecycle events can happen independently in a separate binding/ creation of the service.
When a tile is added by the user its TileService will be bound to and onTileAdded() will be called.
When a tile should be up to date and listing will be indicated by onStartListening() and onStopListening().
When the user removes a tile from Quick Settings onTileRemoved() will be called.
onTileAdded() and onTileRemoved() may be called outside of the onCreate() - onDestroy() window
TileService will be detected by tiles that match the "android. service. quicksettings. action. QS_TILE" and require the permission "android. permission. BIND_QUICK_SETTINGS_TILE". The label and icon for the service will be used as the default label and icon for the tile. Here is an example TileService declaration.
  <service
     android:name=".MyQSTileService"
     android:label="@string/ my_default_tile_label"
     android:icon="@drawable/ my_default_icon_label"
     android:permission="android. permission. BIND_QUICK_SETTINGS_TILE">
     <intent-filter>
         <action android:name="android. service. quicksettings. action. QS_TILE" />
     </ intent-filter>
 </ service>

照葫芦画瓢,

  • icon :自定义图标。
  • label :描述。可以通过 tile.setLabel() 修改。
  • android:permission=“android.permission.BIND_QUICK_SETTINGS_TILE” :固定写法。
  • < action android:name=“android.service.quicksettings.action.QS_TILE” /> :固定写法。

两个固定写法,frameworks/base/packages/SystemUI/docs/qs-tiles.md 里也有说明。

This is an abstract Service that needs to be implemented by the developer. The Service manifest must have the permission android.permission.BIND_QUICK_SETTINGS_TILE and must respond to the action android.service.quicksettings.action.QS_TILE. This will allow SystemUI to find the available tiles and display them to the user.

示例代码,

	<service
            android:name=".tileservice.SwitchTileService"
            android:enabled="true"
            android:exported="true"
            android:icon="@drawable/icon_info"
            android:label="MySwitchLabel"
            android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
            <intent-filter>
                <action android:name="android.service.quicksettings.action.QS_TILE" />
            </intent-filter>
        </service>
        <service
            android:name=".tileservice.ClickTileService"
            android:enabled="true"
            android:exported="true"
            android:icon="@drawable/icon_vector_red_star"
            android:label="MyClickLabel"
            android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
            <intent-filter>
                <action android:name="android.service.quicksettings.action.QS_TILE" />
            </intent-filter>
        </service>

run ,添加到 QS ,两个 Tile 的效果,
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2336477.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【MySQL】前缀索引、索引下推、访问方法,自适应哈希索引

最左前缀原则 对于INDEX(name, age)来说最左前缀可以是联合索引的最左N个字段, 也可以是字符串索引的最左M个字符。 SELECT * FROM t WHERE name LIKE 张%其效果和单独创建一个INDEX(name)的效果是一样的若通过调整索引字段的顺序, 可以少维护一个索引树, 那么这个顺序就是需要…

Android Studio开发知识:从基础到进阶

引言 Android开发作为移动应用开发的主流方向之一&#xff0c;曾吸引了无数开发者投身其中。然而&#xff0c;随着市场饱和和技术迭代&#xff0c;当前的Android开发就业形势并不乐观&#xff0c;竞争日益激烈。尽管如此&#xff0c;掌握扎实的开发技能仍然是脱颖而出的关键。本…

ocr-身份证正反面识别

在阿里云官网&#xff0c;申请一个token [阿里官方]身份证OCR文字识别_API专区_云市场-阿里云 (aliyun.com) 观察一下post请求body部分json字符串&#xff0c;我们根据这个创建一个java对象 先默认是人像面 public class IdentityBody {public String image;class configure…

单节锂电池4.2V升压5V都有哪些国产芯片推荐?国产SL4011高效,高性价比

针对单节锂电池&#xff08;4.2V&#xff09;升压至5V应用中 SL4011升压芯片 的核心优势解析&#xff0c;结合其技术参数与典型应用场景进行详细说明&#xff1a; 1. 宽输入电压与高兼容性 输入范围&#xff1a;2.7V-12V&#xff0c;完美覆盖单节锂电池全周期电压&#xff08;3…

机器学习 | 神经网络介绍 | 概念向

文章目录 &#x1f4da;从生物神经元到人工神经元&#x1f4da;神经网络初识&#x1f407;激活函数——让神经元“动起来”&#x1f407;权重与偏置——调整信息的重要性&#x1f407;训练神经网络——学习的过程&#x1f407;过拟合与正则化——避免“死记硬背” &#x1f440…

视频孪生重构施工逻辑:智慧工地的数字化升级

当"智慧工地"概念在2017年首次写入《建筑业发展"十三五"规划》时&#xff0c;行业普遍将其等同于摄像头与传感器的简单叠加。十年数字浪潮冲刷下&#xff0c;智慧工地的内涵已发生本质跃迁&#xff1a;从工具层面的信息化改造&#xff0c;进化为基于视频数…

六根觉性:穿透表象的清净觉知之光

在喧嚣的禅堂里&#xff0c;老禅师轻叩茶盏&#xff0c;清脆的声响划破沉寂。这声"叮"不仅震动耳膜&#xff0c;更叩击着修行者的心性——这正是佛教揭示的六根觉性在世间万相中的妙用。当我们凝视《楞严经》中二十五圆通法门&#xff0c;六根觉性犹如六道澄明之光&a…

spring:注解@Component、@Controller、@Service、@Reponsitory

背景 spring框架的一个核心功能是IOC&#xff0c;就是将Bean初始化加载到容器中&#xff0c;Bean是如何加载到容器的&#xff0c;可以使用spring注解方式或者spring XML配置方式。 spring注解方式直接对项目中的类进行注解&#xff0c;减少了配置文件内容&#xff0c;更加便于…

Halcon应用:九点标定-手眼标定

提示&#xff1a;若没有查找的算子&#xff0c;可以评论区留言&#xff0c;会尽快更新 Halcon应用&#xff1a;九点标定-手眼标定 前言一、Halcon应用&#xff1f;二、应用实战1、图形理解[eye-to-hand]&#xff1a;1.1、开始应用2 图形理解[eye-in-hand] 前言 本篇博文主要用…

【iOS】OC高级编程 iOS多线程与内存管理阅读笔记——自动引用计数(一)

自动引用计数 前言alloc/retain/release/dealloc实现苹果的实现 autoreleaseautorelease实现苹果的实现 总结 前言 此前&#xff0c;写过一遍对自动引用计数的简单学习&#xff0c;因此掠过其中相同的部分&#xff1a;引用计数初步学习 alloc/retain/release/dealloc实现 由于…

Python爬虫第15节-2025今日头条街拍美图抓取实战

目录 一、项目背景与概述 二、环境准备与工具配置 2.1 开发环境要求 2.2 辅助工具配置 三、详细抓取流程解析 3.1 页面加载机制分析 3.2 关键请求识别技巧 3.3 参数规律深度分析 四、爬虫代码实现 五、实现关键 六、法律与道德规范 一、项目概述 在当今互联网时代&a…

智慧城市像一张无形大网,如何紧密连接你我他?

智慧城市作为复杂巨系统&#xff0c;其核心在于通过技术创新构建无缝连接的网络&#xff0c;使物理空间与数字空间深度融合。这张"无形大网"由物联网感知层、城市数据中台、人工智能中枢、数字服务入口和安全信任机制五大支柱编织而成&#xff0c;正在重塑城市运行规…

网络安全·第四天·扫描工具Nmap的运用

今天我们要介绍网络安全中常用的一种扫描工具Nmap&#xff0c;它被设计用来快速扫描大型网络&#xff0c;主要功能包括主机探测、端口扫描以及版本检测&#xff0c;小编将在下文详细介绍Nmap相应的命令。 Nmap的下载安装地址为&#xff1a;Nmap: the Network Mapper - Free Se…

黑龙江 GPU 服务器租用:开启高效计算新征程

随着人工智能、深度学习、大数据分析等技术的广泛应用&#xff0c;对强大计算能力的需求日益迫切。GPU 服务器作为能够提供卓越并行计算能力的关键设备&#xff0c;在这一进程中发挥着至关重要的作用。对于黑龙江地区的企业、科研机构和开发者而言&#xff0c;选择合适的 GPU 服…

SparseDrive---论文阅读

纯视觉下的稀疏场景表示 算法动机&开创性思路 算法动机&#xff1a; 依赖于计算成本高昂的鸟瞰图&#xff08;BEV&#xff09;特征表示。预测和规划的设计过于直接&#xff0c;没有充分利用周围代理和自我车辆之间的高阶和双向交互。场景信息是在agent周围提取&#xff…

Unchained 内容全面上链,携手 Walrus 迈入去中心化媒体新时代

加密新闻媒体 Unchained — — 业内最受信赖的声音之一 — — 现已选择 Walrus 作为其去中心化存储解决方案&#xff0c;正式将其所有媒体内容&#xff08;文章、播客和视频&#xff09;上链存储。Walrus 将替代 Unchained 现有的中心化存储架构&#xff0c;接管其全部历史内容…

确保连接器后壳高性能互连的完整性

本文探讨了现代后壳技术如何促进高性能互连的电气和机械完整性&#xff0c;以及在规范阶段需要考虑的一些关键因素。 当今的航空航天、国防和医疗应用要求连接器能够提供高速和紧凑的互连&#xff0c;能够承受振动和冲击&#xff0c;并保持对电磁和射频干扰 &#xff08;EMI/R…

C++学习Day0:c++简介

目录 一、.C语言的发展史二、C特点三、面向对象的重要术语四、面向过程和面向对象的区别&#xff1f;五、开发环境&#xff1a;六、创建文件步骤&#xff1a;1.点击新建项目2.在弹出的开始栏中按如下操作3.在.pro文件中添加&#xff08;重要&#xff01;&#xff01;&#xff0…

从零开始构建 Ollama + MCP 服务器

Model Context Protocol&#xff08;模型上下文协议&#xff09;在过去几个月里已经霸占了大家的视野&#xff0c;出现了许多酷炫的集成示例。我坚信它会成为一种标准&#xff0c;因为它正在定义工具与代理或软件与 AI 模型之间如何集成的新方式。 我决定尝试将 Ollama 中的一…

MATLAB学习笔记(二) 控制工程会用到的

MATLAB中 控制工程会用到的 基础传递函数表达传递函数 零极点式 状态空间表达式 相互转化画响应图线根轨迹Nyquist图和bode图现控部分求约旦判能控能观极点配置和状态观测 基础 传递函数表达 % 拉普拉斯变换 syms t s a f exp(a*t) %e的a次方 l laplace(f) …