M版上在状态栏新增截屏功能

1.需求

客户要求在状态栏新增截屏功能。。。本文参考了 http://blog.csdn.net/u012966861/article/details/52511973非常感谢

2.实现步骤

1.步骤1

在配置文件frameworks\base\packages\SystemUI\res\values\config.xml添加\screenshot 显示

 
    
        wifi,bt,inversion,dnd,cell,airplane,rotation,flashlight,location,cast,hotspot,screenshot 
    

2.步骤2

在frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\QSTileHost.java新增如下:

import com.android.systemui.qs.tiles.ScreenShotTile;//
 import com.android.systemui.statusbar.policy.ScreenShotController;//
 private final ScreenShotController mScreenShotController;  //
 public QSTileHost(.....ScreenShotController screenshot)
 {
 ......
 mScreenShotController=screenshot;
 ......
 }
 
  protected QSTile createTile(String tileSpec) 
  {
  ......
  else if(tileSpec.equals("screenshot")) return new ScreenShotTile(this);
  ......
  }
  
  public ScreenShotController getScreenShotController()
{  
    return mScreenShotController; 
}  

3.步骤3

新增一个类 在里面实现截屏功能frameworks\base\packages\SystemUI\src\com\android\systemui\qs\tiles\ScreenShotTile.java

package com.android.systemui.qs.tiles;
import com.android.systemui.qs.QSTile;
import com.android.systemui.R;
import android.os.Message;
import android.os.Handler;
import android.content.Context;
import android.content.Intent;
import com.android.internal.logging.MetricsLogger;
import com.android.systemui.screenshot.GlobalScreenshot;
import android.os.RemoteException;
import com.android.systemui.statusbar.policy.ScreenShotController;
import android.os.Messenger;
public class ScreenShotTile extends QSTile {  
    private static final String TAG = "ScreenShotTile";  
    private static final int SCREEN_SHOT_MESSAGE = 10000;  
    private static GlobalScreenshot mScreenshot;  
	 private final ScreenShotController mScreenShotController;
      
    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(Host host) {  
        super(host);  
		mScreenShotController=host.getScreenShotController();
    }  
  
    @Override  
    protected BooleanState newTileState() {  
        return new BooleanState();  
    }  
  
    @Override  
    public void setListening(boolean listening) {  
    }  
  
    @Override  
    protected void handleClick() {  
        mHost.startActivityDismissingKeyguard(new Intent());  //hide statusbar
        Message msg = mHandler.obtainMessage(SCREEN_SHOT_MESSAGE);  
        mHandler.sendMessageDelayed(msg,1000);  
    }  
    @Override  
    protected void handleLongClick() {  
        mHost.startActivityDismissingKeyguard(new Intent());  //hide statusbar
        Message msg = mHandler.obtainMessage(SCREEN_SHOT_MESSAGE);  
        mHandler.sendMessageDelayed(msg,1000);  
    }  
  
    @Override  
    protected void handleUpdateState(BooleanState state, Object arg) {  
        state.value=false;  
        state.autoMirrorDrawable=false;  
        state.label = mContext.getString(R.string.screenshot);
        state.icon = ResourceIcon.get(R.drawable.ic_qs_screenshot);
	   state.visible = true;
}		
@Override
 public int getMetricsCategory(){ 
return MetricsLogger.QS_SCREEN_SHOT; 
} 
}

4.步骤4

新增一个接口frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\policy/ScreenShotController.java

package com.android.systemui.statusbar.policy;  
  
public interface ScreenShotController{  
        boolean isSupportScreenShot();  
}  

5.步骤5

在frameworks\base\packages\SystemUI\src\com\android\systemui\qs\QSTile.java新增

import com.android.systemui.statusbar.policy.ScreenShotController;  
public interface Host {
......
ScreenShotController getScreenShotController();
......
}

6.步骤6

在frameworks/base/packages/SystemUI/res/values/strings.xml添加

ScreenShot  

7.步骤7

在frameworks/base/packages/SystemUI/res/drawable-xhdpi/新增图片;也可以用矢量画出图片

ic_qs_screenshot.png

8.步骤8

在frameworks/base/packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java修改权限

class GlobalScreenshot 修改为 public
method takeScreenshot 修改为 public

9.步骤9

在frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\PhoneStatusBar.java新增

import com.android.systemui.statusbar.policy.ScreenShotControllerImpl;
 ScreenShotControllerImpl mScreenShotController;
  final QSTileHost qsh = new QSTileHost(......
					mScreenShotController
            );

10.步骤10

实现接口ScreenShotController.java

如下frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\policy\ScreenShotControllerImpl.java
package com.android.systemui.statusbar.policy;  
import android.content.Context;  
public class ScreenShotControllerImpl implements ScreenShotController{  
private static final String TAG="ScreenShotControllerImpl";  
private final Context mContext;  
public ScreenShotControllerImpl(Context context){  
mContext=context;  
}  
@Override  
public boolean isSupportScreenShot(){  
return true;  
}  
}  

11.步骤11

在frameworks\base\packages\SystemUI\src\com\android\systemui\tuner\QsTuner.java

private static class CustomHost extends QSTileHost {

        public CustomHost(Context context) {
            // M: support extra tiles
            super(context, null, null, null, null, null, null, null, null, null,
                    null, null, new BlankSecurityController(), null, null,null);
        }

CustomHost继承了QSTileHost ,并且调用了父类的的构造方法,因为父类的构造方法新增了一个参数,所以要在最后新增一个null参数
到此已经实现啦。。。。。。

你可能感兴趣的:(Android)