在 Android 手机上快速更改设置
您可以在手机的任何屏幕中,通过“快捷设置”菜单查看和更改设置。为了方便访问您经常更改的设置,您可以将这些设置添加或移动到“快捷设置”中。
创建Tile
1.创建您的自定义图标
您需要提供一个自定义图标,该图标显示在“快速设置”面板的磁贴上。(您将在声明 时添加此图标TileService,将在下一节中介绍。)该图标必须是带有透明背景的纯白色,尺寸为 24 x 24dp,格式为 VectorDrawable.
2.创建并声明您的 TileService
TileService
为扩展类的磁贴创建服务。
public class QuickSettingsService extends TileService {
private static final String SERVICE_STATUS_FLAG = "serviceStatus";
private static final String PREFERENCES_KEY = ".android_quick_settings";
/**
* Called when the tile is added to the Quick Settings.
* @return TileService constant indicating tile state
*/
@Override
public void onTileAdded() {
Log.d("QS", "Tile added");
}
/**
* Called when this tile begins listening for events.
*/
@Override
public void onStartListening() {
Log.d("QS", "Start listening");
}
/**
* Called when the user taps the tile.
*/
@Override
public void onClick() {
Log.d("QS", "Tile tapped");
updateTile();
}
/**
* Called when this tile moves out of the listening state.
*/
@Override
public void onStopListening() {
Log.d("QS", "Stop Listening");
}
/**
* Called when the user removes this tile from Quick Settings.
*/
@Override
public void onTileRemoved() {
Log.d("QS", "Tile removed");
}
// Changes the appearance of the tile.
private void updateTile() {
Tile tile = this.getQsTile();
boolean isActive = getServiceStatus();
Icon newIcon;
String newLabel;
int newState;
// Change the tile to match the service status.
if (isActive) {
newLabel = String.format(Locale.US,
"%s %s",
getString(R.string.tile_label),
getString(R.string.service_active));
newIcon = Icon.createWithResource(getApplicationContext(),
ic_android_black_24dp);
newState = Tile.STATE_ACTIVE;
} else {
newLabel = String.format(Locale.US,
"%s %s",
getString(R.string.tile_label),
getString(R.string.service_inactive));
newIcon =
Icon.createWithResource(getApplicationContext(),
android.R.drawable.ic_dialog_alert);
newState = Tile.STATE_INACTIVE;
}
// Change the UI of the tile.
tile.setLabel(newLabel);
tile.setIcon(newIcon);
tile.setState(newState);
// Need to call updateTile for the tile to pick up changes.
tile.updateTile();
}
// Access storage to see how many times the tile
// has been tapped.
private boolean getServiceStatus() {
SharedPreferences prefs =
getApplicationContext()
.getSharedPreferences(PREFERENCES_KEY,
MODE_PRIVATE);
boolean isActive = prefs.getBoolean(SERVICE_STATUS_FLAG, false);
isActive = !isActive;
prefs.edit().putBoolean(SERVICE_STATUS_FLAG, isActive).apply();
return isActive;
}
}
TileService
在应用的清单文件中声明您的。添加您的 的名称和标签、您TileService
在上一节中创建的自定义图标以及适当的权限。
<service
android:name=".MyQSTileService"
android:label="@string/my_default_tile_label" // 18-character limit.
android:icon="@drawable/my_default_icon_label"
android:permission=".BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name=".QS_TILE" />
</intent-filter>
</service>
管理 TileService
生命周期概述
除了控制绑定服务生命周期的回调之外,您还必须实现特定于TileService生命周期的其他方法。这些方法可以在外部调用onCreate(),onDestroy()因为Service 生命周期方法和TileService生命周期方法是在两个单独的异步线程中调用的。
生命TileService周期包含以下方法,每次TileService进入新的生命周期阶段时系统都会调用这些方法:
- onTileAdded():此方法仅在用户第一次添加您的磁贴时调用,并且如果用户删除并再次添加您的磁贴。这是进行任何一次性初始化的最佳时机。但是,这可能无法满足所有需要的初始化。
注意:在创建 tile 时不会调用。例如,如果在设备关闭之前已添加且未移除磁贴,则在设备重新启动或打开电源时不会调用。 onTileAdded()onTileAdded()
-
onStartListening()和onStopListening():这些方法在您的应用更新磁贴时被调用,并且经常被调用。TileService仍然绑定在onStartListening()和 之间,允许您的 onStopListening()应用修改磁贴并推送更新。
-
onTileRemoved():仅当用户移除您的磁贴时才会调用此方法。
注意:这些阶段可能不会连续发生。onTileAdded()当您的磁贴被用户添加到他们的快速设置面板时,只能调用一次。 onStartListening()并且onStopListening()可能在整个生命周期中被多次调用TileService。onTileRemoved()如果用户没有从他们的快速设置面板中删除您的磁贴,将永远不会被调用。
2.平铺状态概述
用户添加您的磁贴后,它始终处于以下状态之一。
- STATE_ACTIVE:表示开启或启用状态。用户可以在此状态下与您的磁贴进行交互。
例如,对于允许用户启动定时锻炼会话的健身应用程序磁贴,STATE_ACTIVE这意味着用户已启动锻炼会话并且计时器正在运行。
- STATE_INACTIVE:表示关闭或暂停状态。用户可以在此状态下与您的磁贴进行交互。
再次使用健身应用程序磁贴示例,磁贴STATE_INACTIVE表示用户尚未启动锻炼会话,但如果他们愿意,可以这样做。
- STATE_UNAVAILABLE: 表示暂时不可用的状态。在此状态下,用户无法与您的磁贴进行交互。
例如,一个磁贴STATE_UNAVAILABLE表示该磁贴由于某种原因当前对用户不可用。
用于STATE_UNAVAILABLE当前不可用但可以稍后进入可用状态的图块。如果该组件将永远不再可供用户使用,请COMPONENT_ENABLED_STATE_DISABLED 传入setComponentEnabledSetting().
系统只设置Tile对象的初始状态。您可以在Tile 对象的剩余生命周期中设置对象的状态。
系统可能会为平铺图标和背景着色以反映 Tile对象的状态。Tile对象设置为STATE_ACTIVE最暗, STATE_INACTIVE越来越亮STATE_UNAVAILABLE。确切的色调特定于制造商和版本。
3.更新您的磁贴
收到回调后,您可以更新您的磁贴onStartListening()。根据磁贴的模式,您的磁贴至少可以更新一次,直到收到对onStopListening().
在活动模式下,您可以在收到回调之前只更新一次磁贴onStopListening()。onStartListening()在非活动模式下,您可以在和之间多次更新您的磁贴onStopListening()。
注意:活动模式允许您更新您的图块,无论您的图块是否对用户可见。
您可以通过调用来检索您的Tile对象getQsTile()。要更新对象的特定字段,请Tile调用以下方法:
setContentDescription()
setIcon()
setLabel()
setState()
setStateDescription()
setSubtitle()
应用清单文件中设置的图标和标签是“快速设置”面板中磁贴上显示的默认值。但是,您可以 在更新磁贴以将字段设置为新值时调用setIcon()和setLabel()方法。将对象的字段设置为正确的值后,您必须调用updateTile()以更新您的磁贴。Tile这将使系统解析更新的图块数据并更新 UI。
相关参考:
/develop/ui/views/quicksettings-tiles