Android Nougat快速设置图块
在本教程中,我们将学习如何在快速设置中实施我们自己的自定义图块。
自Android Nougat以来,这已经成为可能。
在下面的android应用程序中,我们将创建一个计数器系统,该系统将对您打开快速设置的次数进行计数。
它将在我们将要实现的"快速设置图块"中显示。
快速设置图块
Android系统提供了一组预定义的图块,这些图块是可以轻松访问我们智能手机中某些功能的开关。
通过轻松访问这些开关,我们可以切换诸如wifi,蓝牙,移动数据,GPS等功能。
以下是它的外观:
单击排序按钮将显示所有图块的列表。
您可以将磁贴拖动到通知托盘中将显示的前10个可见磁贴中。
快速设置图块是与Android Nougat一起发布的新API。
磁贴主要具有以下三种状态:
- STATE_ACTIVE
- STATE_INACTIVE
- STATE_UNAVAILABLE –在这种情况下,在图块上禁用了单击操作
要在我们的android应用程序中创建图块,我们需要创建一个扩展TileService的服务。
TileService是绑定服务。
因此,它与Android系统相关。
您需要在AndroidManifest.xml文件中添加具有权限的TileService。
<service
android:name=".MyTileService"
android:icon="@drawable/ic_tile_default"
android:label="@string/tile_name"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action
android:name="android.service.quicksettings.action.QS_TILE"
</intent-filter>
</service>
TileService具有四个重要方法:
onClick()–单击磁贴时。onStartListening()–打开通知托盘时onStopListening()–关闭通知托盘时。
onTileAdded()–将图块添加到快速图块中时。
第一次安装该应用程序时会发生这种情况。
其中您可以在图块的初始状态下对其进行设置。onTileRemoved()-从快速磁贴上移除磁贴时。
我们必须设置图块的UI。
磁贴具有三个重要组成部分:
- 文本
- 图标
- 州
设置完这些之后,必须在Tile实例上调用updateTile()来更新"快速设置"中的UI。
我们可以通过在TileService类的onClick()方法中创建intent方法来启动活动。
" startActivity"只会启动活动,但不会关闭保存"快速设置"的通知列。
为了关闭托盘,调用startActivityAndCollapse方法。
它将折叠通知托盘。
在下一节中,我们将创建一个Android应用程序,该应用程序每次打开"快速设置"时都会增加一个计数器变量。
它显示打开快速设置的次数。
跟踪生产率以及打开快速设置多少次的一种好方法。
我们会将计数存储在SharedPreferences中。
代码
使用以下代码更新您的AndroidManifest.xml:
<activity
android:name=".MainActivity"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN"
<category android:name="android.intent.category.LAUNCHER"
</intent-filter>
</activity>
<service
android:name=".MyTileService"
android:icon="@android:drawable/ic_media_pause"
android:label="Disabled"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE"
</intent-filter>
</service>
在上方的"服务"中,我们设置了图块的图标和标签。
在活动代码内部,我们将其设置为单个实例。
这样可以防止每次启动startActivity时都创建活动的多个实例。
下面给出了MyTileService.java类的代码:
package com.theitroad.androidquicksettingstitle;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Icon;
import android.preference.PreferenceManager;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import android.util.Log;
import android.widget.Toast;
public class MyTileService extends TileService {
SharedPreferences mSharedPreferences;
@Override
public void onClick() {
super.onClick();
Tile tile = getQsTile();
boolean isActive = (tile.getState() == Tile.STATE_ACTIVE);
if (isActive) {
tile.setState(Tile.STATE_INACTIVE);
startActivityAndCollapse(new Intent(this, MainActivity.class));
tile.setLabel("Disabled");
tile.setContentDescription("Test App");
tile.setIcon(Icon.createWithResource(this, android.R.drawable.ic_media_play));
} else {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int counter = mSharedPreferences.getInt("counter", 0);
tile.setState(Tile.STATE_ACTIVE);
tile.setIcon(Icon.createWithResource(this, android.R.drawable.ic_media_pause));
tile.setLabel("Count " + counter);
}
tile.updateTile();
}
@Override
public void onTileAdded() {
super.onTileAdded();
Tile tile = getQsTile();
tile.setState(Tile.STATE_ACTIVE);
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int counter = mSharedPreferences.getInt("counter", 0);
tile.setLabel("Count " + counter);
tile.setIcon(Icon.createWithResource(this, android.R.drawable.ic_media_pause));
tile.updateTile();
Toast.makeText(getApplicationContext(), "tile added", Toast.LENGTH_SHORT).show();
}
@Override
public void onTileRemoved() {
super.onTileRemoved();
}
@Override
public void onStartListening() {
super.onStartListening();
Tile tile = getQsTile();
if (tile.getState() == Tile.STATE_ACTIVE) {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int counter = mSharedPreferences.getInt("counter", 0);
mSharedPreferences.edit().putInt("counter", ++counter).apply();
tile.setLabel("Count " + counter);
}
tile.updateTile();
}
@Override
public void onStopListening() {
super.onStopListening();
}
}
在" onStartListening"内部,我们检查状态是否为活动状态。
如果是这样,我们将更新计数器并保存其共享首选项。
在" onClick()"内部,我们开始活动并关闭"快速设置"通知托盘。
" Tile tile = getQsTile();"用于检索Tile实例。
下面给出了activity_main.xml布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
</android.support.constraint.ConstraintLayout>
MainActivity.java类的代码如下:
package com.theitroad.androidquicksettingstitle;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int counter = sharedPreferences.getInt("counter", 0);
textView.setText("You have opened the quick settings " + counter + " times.");
}
@Override
protected void onResume() {
super.onResume();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int counter = sharedPreferences.getInt("counter", 0);
textView.setText("You have opened the quick settings " + counter + " times.");
}
}
在onResume()内部,我们使用SharedPreferences中的最新计数器值更新TextView。
每当在Activity已经运行的情况下执行startActivity时,都会调用onNewIntent()。

