之前在6.0上做过这个功能,8.1对比6.0代码上有部分重构,增加按钮也方便了很多,6.0需要添加一个TileController,这个controller是一个interface,还要一个TileControllerImpl,类似截图这样的按钮其实在controller中是没有逻辑的,但是也必须要有controller,不是很友好。Google工程师可能也是考虑了这一点,8.1重构了这部分的代码。
首先在配置文件中增加screenshot
res/values/config.xml :
<string name="quick_settings_tiles_stock" translatable="false">
wifi,cell,battery,dnd,flashlight,rotation,bt,airplane,location,hotspot,inversion,saver,work,cast,night,screenshot
string>
8.1增加了修改qs的的功能,这样增加默认是不显示出来的,需要在手机设设置一下。其实这么默认选项也是读取配置文件的
res/values/config.xml :
<string name="quick_settings_tiles_default" translatable="false">
wifi,cell,bt,dnd,flashlight,rotation,battery,airplane,cast,screenshot
string>
增加一个ScreenshotTile.java
public class ScreenshotTile extends QSTileImpl {
private static final String TAG = "ScreenShotTile";
private static final int SCREEN_SHOT_MESSAGE = 10000;
private static GlobalScreenshot mScreenshot;
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SCREEN_SHOT_MESSAGE:
final Messenger callback = msg.replyTo;
if (mScreenshot == null) {
mScreenshot = new GlobalScreenshot(mContext);
}
mScreenshot.takeScreenshot(new Runnable() {
@Override public void run() {
Message reply = Message.obtain(null, 1);
try {
if(callback != null){
callback.send(reply);
}
}catch(RemoteException e){
}
}
}, msg.arg1 > 0, msg.arg2 > 0);
break;
default:
break;
}
}
};
public ScreenshotTile(QSHost host) {
super(host);
}
@Override
public BooleanState newTileState() {
return new BooleanState();
}
@Override
protected void handleClick() {
mHost.collapsePanels();
Message msg = mHandler.obtainMessage(SCREEN_SHOT_MESSAGE);
mHandler.sendMessageDelayed(msg,500);
}
@Override
protected void handleUpdateState(BooleanState state, Object arg) {
state.icon = ResourceIcon.get(R.drawable.screenshot);
state.label = mContext.getString(R.string.quick_settings_screen_shot);
state.contentDescription = mContext.getString(R.string.quick_settings_screen_shot);
}
@Override
public int getMetricsCategory() {
return 0;
}
@Override
public Intent getLongClickIntent() {
return null;
}
@Override
protected void handleSetListening(boolean listening) {
}
@Override
public CharSequence getTileLabel() {
return mContext.getString(R.string.quick_settings_screen_shot);
}
}
在创建Tile的地方添加ScreenShotTile
src/com/android/systemui/qs/tileimpl/QSFactoryImpl.java -> createTile():
else if (tileSpec.equals("nfc")) return new NfcTile(mHost);
//增加ScreenShotTile begin
else if (tileSpec.equals("screenshot")) return new ScreenshotTile(mHost);
//增加ScreenShotTile end
/// M: Customize the quick settings tiles for operator. @{
else if (tileSpec.equals("dataconnection") && !SIMHelper.isWifiOnlyDevice())
return new MobileDataTile(mHost);
运行一下,看看是不是已经添加好了呢。