Android4.0上添加截屏快捷方式

拿到Samsung的7寸平板,是3.x系统,在状态栏上多了个截屏按钮,拿到Galaxy Notes 10.1,该功能更是不可删除的添加在状态栏,既然如此,我们今天在我们平板上也添加上该按键,实现简单的快捷截屏功能,而不是按POWER和VOL-进行。
     在Android4.0源码的frameworks/base/packages/SystemUI/res/drawable-mdpi/目录下添加ic_sysbar_screenshot.png文件,图片如下:
     Android4.0上添加截屏快捷方式
     该图片的背景是透明的PNG文件。
     接下来修改frameworks/base/packages/SystemUI/res/layout-sw600dp/status_bar.xml文件,在
     
                    android:layout_width="80dip"
                    android:layout_height="match_parent"
                    android:src="@drawable/ic_sysbar_screenshot"
                    android:contentDescription="@string/accessibility_screenshot"
                    systemui:glowBackground="@drawable/ic_sysbar_highlight"
                    />
     接下来修改frameworks/base/packages/SystemUI/res/values/strings.xml文件,添加如下内容:
     ScreenShot
     至此,重新编译烧录源码,在状态栏上会多出截屏图标,接下来需要继续实现截屏功能,查看了frameworks里的源码,找到了frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java文件里有可参考的片断,故继续修改frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java文件,相应的添加如下内容:
      import android.os.Messenger;
      import android.content.ComponentName;
      import android.content.ServiceConnection;
      在View mRecentButton;后面添加View mScreenshotButton;语句。
      在mRecentButton.setOnClickListener(mOnClickListener);后添加如下内容:
      mScreenshotButton = mNavigationArea.findViewById(R.id.my_screenshot);
        mScreenshotButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHandler.postDelayed(mScreenshotChordLongPress,
                        ViewConfiguration.getGlobalActionKeyTimeout());
            }
        });
       接下来在public int getStatusBarHeight() {前添加如下内容:
       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;
                }
            }
        }
    };

    // Assume this is called from the Handler thread.
    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;
                        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);
            }
        }
    }
    至此,再次编译源码并烧录,截屏功能实现了,现在还存有的是图标的状态处理了,这部分就不说明了。
    实现截屏功能时,4.0上既然已实现该功能,那么就直接找源码,确认哪儿有实现,直接拿过来使用,尽量拿来主义,但又不失学习性。

你可能感兴趣的:(Android4.0上添加截屏快捷方式)