Android4.0 状态栏添加截屏功能按钮

1>frameworks\base\packages\SystemUI\res\layout-sw600dp\status_bar.xml:添加

<com.android.systemui.statusbar.policy.KeyButtonView 
            android:id="@+id/sysbar_screenshot"
            android:layout_width="80dip"
            android:layout_height="match_parent"
            android:src="@drawable/ic_sysbar_screenshot" //搞个摄像头模样的图片放在frameworks\base\packages\SystemUI\res\drawable-mdpi
            systemui:glowBackground="@drawable/ic_sysbar_highlight" />

2>frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\tablet\TabletStatusBar.java:添加

import android.content.ServiceConnection;
import android.content.ComponentName;
import android.os.Messenger;
import android.util.Log;
import android.os.ServiceManager;

View mPrintScreenButton;//cao

protected View makeStatusBarView() {
。。。。。。。
mPrintScreenButton = mNavigationArea.findViewById(R.id.sysbar_screenshot);
mPrintScreenButton.setOnClickListener(mOnClickListener);
。。。。。。。


private void setNavigationVisibility(int visibility) {
。。。。。。。
mPrintScreenButton.setVisibility(View.VISIBLE);


private View.OnClickListener mOnClickListener = new View.OnClickListener() {
。。。。。。。
else if(v == mPrintScreenButton){
            new Thread(new Runnable() {  
                    @Override  
                    public void run() {  
                        takeScreenshot();  
                    }  
                }).start();  
            
         }
另外加上以下代码(截屏功能属于Android4.0自带功能,以下代码在frameworks\base\policy\src\com\android\internal\policy\impl\PhoneWindowManager.java可找到)。

private void takeScreenshot() {
   synchronized (mScreenshotLock) {           
       if (mScreenshotConnection != null) {                
        return;            
        }            
    ComponentName cn = new ComponentName("com.android.systemui", "com.android.systemui.screenshot.TakeScreenshotService");            
    Intent intent = new Intent();            
    intent.setComponent(cn);           
    ServiceConnection conn = new ServiceConnection() {                
        @Override                
            public void onServiceConnected(ComponentName name, IBinder service) {                    
            synchronized (mScreenshotLock) {                        
                if (mScreenshotConnection != this) {                            
                    return;                        
                    }                        
                Messenger messenger = new Messenger(service);                       
                Message msg = Message.obtain(null, 1);                        
                final ServiceConnection myConn = this;                       
                Handler h = new Handler(mHandler.getLooper()) {                            
                    @Override                            
                        public void handleMessage(Message msg) {                               
                        synchronized (mScreenshotLock) {                                    
                            if (mScreenshotConnection == myConn) {                                        
                                mContext.unbindService(mScreenshotConnection);                                        
                                mScreenshotConnection = null;                                        
                                mHandler.removeCallbacks(mScreenshotTimeout);                                   
                                }                                
                            }                           
                        }                       
                    };                        
                msg.replyTo = new Messenger(h);                        
                msg.arg1 = msg.arg2 = 0;                        
                //if (mStatusBar != null && mStatusBar.isVisibleLw())                            
                    // msg.arg1 = 1;                       
                //if (mNavigationBar != null && mNavigationBar.isVisibleLw())                            
                    //msg.arg2 = 1;                        
                try {                            
                    messenger.send(msg);                        
                    } catch (RemoteException e) {                       
                    }                   
                }                
            }                
        @Override                
            public void onServiceDisconnected(ComponentName name) {}            
        };            
    if (mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE)) {                
        mScreenshotConnection = conn;                
        mHandler.postDelayed(mScreenshotTimeout, 10000);            
        }       
    }   
   }
private final Runnable mScreenshotChordLongPress = new Runnable() {       
    public void run() {            
        takeScreenshot();        
        }   
    };
final Object mScreenshotLock = new Object();    
ServiceConnection mScreenshotConnection = null;
final Runnable mScreenshotTimeout = new Runnable() {        
    @Override public void run() {            
        synchronized (mScreenshotLock) {               
            if (mScreenshotConnection != null) {                    
                mContext.unbindService(mScreenshotConnection);                    
                mScreenshotConnection = null;               
                }            
            }       
        }   
    };






你可能感兴趣的:(Android4.0 状态栏添加截屏功能按钮)