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

拿到Samsung的7寸平板,是3.x系統(tǒng),在狀態(tài)欄上多了個(gè)截屏按鈕,拿到Galaxy Notes 10.1,該功能更是不可刪除的添加在狀態(tài)欄,既然如此,我們今天在我們平板上也添加上該按鍵,實(shí)現(xiàn)簡(jiǎn)單的快捷截屏功能,而不是按POWER和VOL-進(jìn)行。

在Android4.0源碼的frameworks/base/packages/SystemUI/res/drawable-mdpi/目錄下添加ic_sysbar_screenshot.png文件,圖片如下:

該圖片的背景是透明的PNG文件。

接下來(lái)修改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"

/>

接下來(lái)修改frameworks/base/packages/SystemUI/res/values/strings.xml文件,添加如下內(nèi)容:

ScreenShot

至此,重新編譯燒錄源碼,在狀態(tài)欄上會(huì)多出截屏圖標(biāo),接下來(lái)需要繼續(xù)實(shí)現(xiàn)截屏功能,查看了frameworks里的源碼,找到了frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java文件里有可參考的片斷,故繼續(xù)修改frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java文件,相應(yīng)的添加如下內(nèi)容:

import android.os.Messenger;

import android.content.ComponentName;

import android.content.ServiceConnection;

在View mRecentButton;后面添加View mScreenshotButton;語(yǔ)句。

在mRecentButton.setOnClickListener(mOnClickListener);后添加如下內(nèi)容:

mScreenshotButton = mNavigationArea.findViewById(R.id.my_screenshot);

mScreenshotButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mHandler.postDelayed(mScreenshotChordLongPress,

ViewConfiguration.getGlobalActionKeyTimeout());

}

});

接下來(lái)在public int getStatusBarHeight() {前添加如下內(nèi)容:

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);

}

}

}

至此,再次編譯源碼并燒錄,截屏功能實(shí)現(xiàn)了,現(xiàn)在還存有的是圖標(biāo)的狀態(tài)處理了,這部分就不說(shuō)明了。

實(shí)現(xiàn)截屏功能時(shí),4.0上既然已實(shí)現(xiàn)該功能,那么就直接找源碼,確認(rèn)哪兒有實(shí)現(xiàn),直接拿過(guò)來(lái)使用,盡量拿來(lái)主義,但又不失學(xué)習(xí)性。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容