本篇文章給大家分享的是有關(guān)Android6.0 開發(fā)中怎么實(shí)現(xiàn)一個固定屏幕功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
公司主營業(yè)務(wù):成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出安徽免費(fèi)做網(wǎng)站回饋大家。
Android 固定屏幕功能
可能大家看到這個標(biāo)題不知道是什么東西,我先說明下,android6.0在設(shè)置->安全->屏幕固定開啟后,然后再長按home鍵出現(xiàn)最近的幾個Activity可以選擇一個圖釘按鈕就開啟了屏幕固定功能。
屏幕固定開啟后,屏幕只能固定在設(shè)定的Task上的Activity切換。
一、設(shè)置固定屏幕
我們先來看SystemUI/src/com/Android/systemui/recents/ScreenPinningRequest.Java的代碼,這段代碼就是長按home鍵出現(xiàn)幾個Activity,然后按了圖釘?shù)哪莻€按鈕。在這里直接調(diào)用了AMS的startLockTaskModeOnCurrent函數(shù)。
@Override public void onClick(View v) { if (v.getId() == R.id.screen_pinning_ok_button || mRequestWindow == v) { try { ActivityManagerNative.getDefault().startLockTaskModeOnCurrent(); } catch (RemoteException e) {} } clearPrompt(); }
我們來看AMS的startLockTaskModeOnCurrent函數(shù),先調(diào)用ActivityStackSupervisor的topRunningActivityLocked獲取最前面的Activity,然后調(diào)用startLockTaskModeLocked函數(shù),參數(shù)是TaskRecord。
public void startLockTaskModeOnCurrent() throws RemoteException { enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS, "startLockTaskModeOnCurrent"); long ident = Binder.clearCallingIdentity(); try { synchronized (this) { ActivityRecord r = mStackSupervisor.topRunningActivityLocked(); if (r != null) { startLockTaskModeLocked(r.task); } } } finally { Binder.restoreCallingIdentity(ident); } }
我們再來看topRunningActivityLocked函數(shù),先從mFocusedStack中獲取最前面的Activity。如果沒有再遍歷所有的mStacks獲取。
ActivityRecord topRunningActivityLocked() { final ActivityStack focusedStack = mFocusedStack; ActivityRecord r = focusedStack.topRunningActivityLocked(null); if (r != null) { return r; } // Return to the home stack. final ArrayListstacks = mHomeStack.mStacks; for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) { final ActivityStack stack = stacks.get(stackNdx); if (stack != focusedStack && isFrontStack(stack)) { r = stack.topRunningActivityLocked(null); if (r != null) { return r; } } } return null; }
在startLockTaskModeLocked函數(shù)中主要是調(diào)用了ActivityStackSupervisor的setLockTaskModeLocked函數(shù),下面我們來看這個函數(shù),我們的task不為null,第一次mLockTaskModeTasks為空,會發(fā)送一個LOCK_TASK_START_MSG消息
void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason, boolean andResume) { if (task == null) { // Take out of lock task mode if necessary final TaskRecord lockedTask = getLockedTaskLocked(); if (lockedTask != null) { removeLockedTaskLocked(lockedTask); if (!mLockTaskModeTasks.isEmpty()) { // There are locked tasks remaining, can only finish this task, not unlock it. if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Tasks remaining, can't unlock"); lockedTask.performClearTaskLocked(); resumeTopActivitiesLocked(); return; } } if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4)); return; } // Should have already been checked, but do it again. if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) { if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Can't lock due to auth"); return; } if (isLockTaskModeViolation(task)) { Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task."); return; } if (mLockTaskModeTasks.isEmpty()) { // First locktask. final Message lockTaskMsg = Message.obtain(); lockTaskMsg.obj = task.intent.getComponent().getPackageName(); lockTaskMsg.arg1 = task.userId; lockTaskMsg.what = LOCK_TASK_START_MSG;//發(fā)送消息 lockTaskMsg.arg2 = lockTaskModeState; mHandler.sendMessage(lockTaskMsg); } // Add it or move it to the top. if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task + " Callers=" + Debug.getCallers(4)); mLockTaskModeTasks.remove(task); mLockTaskModeTasks.add(task);//加入到mLockModeTasks中 if (task.mLockTaskUid == -1) { task.mLockTaskUid = task.effectiveUid; } if (andResume) { findTaskToMoveToFrontLocked(task, 0, null, reason);//把task放最前面 resumeTopActivitiesLocked();//顯示新的Activity } }
我們再來看消息處理,在消息處理中主要調(diào)用了WMS的disableKeyguard函數(shù)。
case LOCK_TASK_START_MSG: { // When lock task starts, we disable the status bars. try { if (mLockTaskNotify == null) { mLockTaskNotify = new LockTaskNotify(mService.mContext); } mLockTaskNotify.show(true); mLockTaskModeState = msg.arg2; if (getStatusBarService() != null) { int flags = 0; if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) { flags = StatusBarManager.DISABLE_MASK & (~StatusBarManager.DISABLE_BACK); } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) { flags = StatusBarManager.DISABLE_MASK & (~StatusBarManager.DISABLE_BACK) & (~StatusBarManager.DISABLE_HOME) & (~StatusBarManager.DISABLE_RECENT); } getStatusBarService().disable(flags, mToken, mService.mContext.getPackageName()); } mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG); if (getDevicePolicyManager() != null) { getDevicePolicyManager().notifyLockTaskModeChanged(true, (String)msg.obj, msg.arg1); } } catch (RemoteException ex) { throw new RuntimeException(ex); } } break;
二、固定屏幕后Activity啟動流程
在固定屏幕后,如果我們啟動其他TaskRecord的Activity是不能啟動的,我們來看下這個原理。在startActivityUncheckedLocked函數(shù)中會調(diào)用isLockTaskModeViolation函數(shù)來判斷是否進(jìn)一步的Activity的啟動流程,我們來看下這個函數(shù),調(diào)用getLockedTaskLocked來看mLockTaskModeTasks(就是鎖定屏幕的那些Task),如果當(dāng)前的task就是當(dāng)前正在固定屏幕的task,直接return false就是可以繼續(xù)啟動Activity的流程,而如果不是,我們需要看task的mLockTaskAuth變量。
boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) { if (getLockedTaskLocked() == task && !isNewClearTask) { return false; } final int lockTaskAuth = task.mLockTaskAuth; switch (lockTaskAuth) { case LOCK_TASK_AUTH_DONT_LOCK: return !mLockTaskModeTasks.isEmpty(); case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: case LOCK_TASK_AUTH_LAUNCHABLE: case LOCK_TASK_AUTH_WHITELISTED: return false; case LOCK_TASK_AUTH_PINNABLE: // Pinnable tasks can't be launched on top of locktask tasks. return !mLockTaskModeTasks.isEmpty(); default: Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth); return true; } }
我們再來看TaskRecord的setLockedTaskAuth函數(shù),在新建一個TaskRecord的時候會調(diào)用setIntent函數(shù),而setIntent函數(shù)又是在TaskRecord的構(gòu)造函數(shù)中調(diào)用的。我們來看這個函數(shù)mLockTaskAuth的值是根據(jù)mLockTaskMode來定的,而mLockTaskMode又是ActivityInfo傳入的,這個值是在PKMS解析AndroidManifest.xml的時候構(gòu)造的,默認(rèn)就是LOCK_TASK_LAUNCH_MODE_DEFAULT,而當(dāng)沒有白名單mLockTaskAuth最后就是LOCK_TASK_AUTH_PINNABLE。
void setLockTaskAuth() { if (!mPrivileged && (mLockTaskMode == LOCK_TASK_LAUNCH_MODE_ALWAYS || mLockTaskMode == LOCK_TASK_LAUNCH_MODE_NEVER)) { // Non-priv apps are not allowed to use always or never, fall back to default mLockTaskMode = LOCK_TASK_LAUNCH_MODE_DEFAULT; } switch (mLockTaskMode) { case LOCK_TASK_LAUNCH_MODE_DEFAULT: mLockTaskAuth = isLockTaskWhitelistedLocked() ? LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE; break; case LOCK_TASK_LAUNCH_MODE_NEVER: mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK; break; case LOCK_TASK_LAUNCH_MODE_ALWAYS: mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV; break; case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED: mLockTaskAuth = isLockTaskWhitelistedLocked() ? LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE; break; } if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this + " mLockTaskAuth=" + lockTaskAuthToString()); }
我們再來看isLockTaskModeViolation函數(shù)如下代碼,現(xiàn)在是task的mLockTaskAuth 是LOCK_TASK_AUTH_PINNABLE,而當(dāng)前處于固定屏幕,所以mLockTaskModeTasks不為null,最后返回true。那Activity啟動流程就不能走下去了,那就是代表啟動普通的Activity會被阻止。
case LOCK_TASK_AUTH_PINNABLE: // Pinnable tasks can't be launched on top of locktask tasks. return !mLockTaskModeTasks.isEmpty();
三、取消固定屏幕
最后我們再來看看取消固定屏幕,取消屏幕會在PhoneStatusBar中取消,但是一定是要有虛擬鍵,原生就是這么設(shè)定的。最后調(diào)用了AMS的stopLockTaskModeOnCurrent函數(shù)。這個函數(shù)主要是調(diào)用了stopLockTaskMode函數(shù),這個函數(shù)中主要是調(diào)用了ActivityStackSupervisor的setLockTaskModeLocked函數(shù),之前在固定屏幕時也是調(diào)用了這個函數(shù),但是這里我們仔細(xì)看,其第一個參數(shù)為null。
public void stopLockTaskMode() { final TaskRecord lockTask = mStackSupervisor.getLockedTaskLocked(); if (lockTask == null) { // Our work here is done. return; } final int callingUid = Binder.getCallingUid(); final int lockTaskUid = lockTask.mLockTaskUid; // Ensure the same caller for startLockTaskMode and stopLockTaskMode. // It is possible lockTaskMode was started by the system process because // android:lockTaskMode is set to a locking value in the application manifest instead of // the app calling startLockTaskMode. In this case {@link TaskRecord.mLockTaskUid} will // be 0, so we compare the callingUid to the {@link TaskRecord.effectiveUid} instead. if (getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_LOCKED && callingUid != lockTaskUid && (lockTaskUid != 0 || (lockTaskUid == 0 && callingUid != lockTask.effectiveUid))) { throw new SecurityException("Invalid uid, expected " + lockTaskUid + " callingUid=" + callingUid + " effectiveUid=" + lockTask.effectiveUid); } long ident = Binder.clearCallingIdentity(); try { Log.d(TAG, "stopLockTaskMode"); // Stop lock task synchronized (this) { mStackSupervisor.setLockTaskModeLocked(null, ActivityManager.LOCK_TASK_MODE_NONE, "stopLockTask", true); } } finally { Binder.restoreCallingIdentity(ident); } }
我們來看下這個函數(shù),如果為空,現(xiàn)在調(diào)用getLockedTaskLocked獲取當(dāng)前固定屏幕的TaskRecord,然后調(diào)用removeLockedTaskLocked去除這個TaskRecord,如果還不為null,調(diào)用resumeTopActivitiesLocked啟動下個Activity(一般也就是下個屏幕鎖定的TaskRecord的Activity)。
如果為空了,直接返回。但是在我們下次啟動普通的Activity的時候就恢復(fù)正常了,因?yàn)閙LockTaskModeTasks已經(jīng)為空了。
void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason, boolean andResume) { if (task == null) { // Take out of lock task mode if necessary final TaskRecord lockedTask = getLockedTaskLocked(); if (lockedTask != null) { removeLockedTaskLocked(lockedTask); if (!mLockTaskModeTasks.isEmpty()) { // There are locked tasks remaining, can only finish this task, not unlock it. if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Tasks remaining, can't unlock"); lockedTask.performClearTaskLocked(); resumeTopActivitiesLocked(); return; } } if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4)); return; }
四、沒有虛擬鍵如何取消屏幕固定
前面說過如果沒有虛擬鍵就不能取消屏幕固定了,我們說下幾種方式
1.使用am命令 am task lock stop可以調(diào)用am的stopLockTaskMode函數(shù)
2.另一種我們可以在Activity.java中修改代碼,比較長按返回鍵調(diào)用AMS的stopLockTaskMode方法,下面就是實(shí)現(xiàn),Activity本身提供了stopLockTask就是調(diào)用了AMS的stopLockTaskMode方法
public boolean onKeyLongPress(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { stopLockTask(); } return false; }
3.直接在Settings中對這項(xiàng)進(jìn)行置灰處理
在SecuritySettings會讀取security_settings_misc.xml文件然后加入相關(guān)perference,這其中就會有如下是屏幕固定相關(guān)的
我們可以在SecuritySettings讀取該文件之后,調(diào)用WMS的hasNavigationBar來看有沒有虛擬鍵(沒有虛擬按鍵到時候不能取消屏幕固定),如果沒有直接把Settings中這項(xiàng)置灰。
// Append the rest of the settings addPreferencesFromResource(R.xml.security_settings_misc); IWindowManager windowManager = WindowManagerGlobal.getWindowManagerService(); try { boolean is_screen_pining = windowManager.hasNavigationBar(); root.findPreference(KEY_SCREEN_PINNING).setEnabled(is_screen_pining); } catch(RemoteException e) { Log.e("SecuritySettings", "get window service remoteException."); }
以上就是Android6.0 開發(fā)中怎么實(shí)現(xiàn)一個固定屏幕功能,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。