Android TileService 使用

TileService API:

  • Google Developer TileService Doc

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.

tileService Api 从Android N 开始提供,用来设置app快捷设置按钮。 如下图示:

这个服务不需要程序开启,系统默认能够识别,并调用,这是一个特殊的Service,在Android 7 加的,在低系统上,则不会触发添加这个操作开关。

image.png
image.png

TileService 使用:

  1. 添加声明:

    
                
                    
                
    
                
            
    

菜单栏中会自动添加此Tile,直接将 Tile 拖动到顶部设置栏中即可:

2. 完整code:

package com.alerm.manager.service

import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Configuration
import android.graphics.drawable.Icon
import android.os.Build
import android.os.IBinder
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import android.util.Log
import androidx.annotation.RequiresApi
import com.play.mi10notify.R
import com.tencent.mmkv.MMKV

@RequiresApi(Build.VERSION_CODES.N)
class MyQsTileService : TileService() {

    companion object {
        val TAG = "MyQsTileService"
    }

    override fun onCreate() {
        super.onCreate()
    }


    override fun startActivity(intent: Intent?) {
        super.startActivity(intent)

    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onBind(intent: Intent?): IBinder? {
        return super.onBind(intent)
    }

    override fun onRebind(intent: Intent?) {
        super.onRebind(intent)
    }

    /**
     * TODO: 按钮开启/关闭操作
     */
    override fun onClick() {
        super.onClick()
        Log.d(TAG, "TileService onClick ++++++++++++++")

        // 更新 Tile 状态
        val open = MMKV.defaultMMKV().decodeBool("open", false)
        qsTile.state = if (open) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
        qsTile.updateTile()
        MMKV.defaultMMKV().encode("open", !open)

        qsTile.icon = Icon.createWithResource(this, R.drawable.ic_baseline_architecture_24)
        qsTile.label = "Studio"
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            qsTile.subtitle = "This is a TileService"
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }


    /**
     * 下拉菜单时被调用,Tile 没有从 Editor 栏拖到设置栏中,则不会调用
     *  onTileAdded() 时会被调用一次
     */
    override fun onStartListening() {
        super.onStartListening()
    }

    /**
     * 同上 同理
     */
    override fun onStopListening() {
        super.onStopListening()
    }

    /**
     * 用户将 Tile 从 Edit 中添加到设定栏中
     */
    override fun onTileAdded() {
        super.onTileAdded()
    }

    /**
     * 将 Tile 移除设定栏
     */
    override fun onTileRemoved() {
        super.onTileRemoved()
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
    }

    override fun onLowMemory() {
        super.onLowMemory()
    }

    override fun onTaskRemoved(rootIntent: Intent?) {
        super.onTaskRemoved(rootIntent)
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
    }

    override fun onUnbind(intent: Intent?): Boolean {
        return super.onUnbind(intent)
    }
}

Google 原生系统手机是支持的,samsung OS 比较接近原生了,应该支持, MIUI 目前高版本OS也支持自定义快捷按钮,onePlus OS 也是支持的。

这是 android N 新功能Quick Settings Tile(快速设置图块), 理论上各大系统厂商应该是均支持此 api 的。

参考:

  1. Quick Settings Tiles on Android 7.0 Ian Lake 关于什么情况下才设置此快捷图块,以及如何实现

  2. Android Tile Service 快速设置

  3. Android SystemUI

  4. Android TileService

你可能感兴趣的:(Android TileService 使用)