欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android如何實現(xiàn)鎖屏狀態(tài)下彈窗

 更新時間:2016年08月12日 17:52:18   作者:單刀土豆  
在鎖屏狀態(tài)下彈窗的效果我們平時并不少見,如QQ、微信和鬧鐘等,但是Android開發(fā)者要怎么實現(xiàn)這一功能呢?下面一起來看看。

前言

想在鎖屏上面實現(xiàn)彈窗,第一個想法就是利用 WindowManager 設(shè)置 Window Flag,通過設(shè)置 Flag 的顯示優(yōu)先級來讓窗口顯示在鎖屏的上面。

接下來就是試驗可能相關(guān)的 Window Type 屬性,驗證該方案是否可行。

在嘗試各個 Window Type 屬性之前需要明確各個 Type 所需要的權(quán)限,下面是 com.android.internal.policy.impl.PhoneWindowManager.checkAddPermission 的源碼:

public int checkAddPermission(WindowManager.LayoutParams attrs) {
  int type = attrs.type;

  if (type < WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW
      || type > WindowManager.LayoutParams.LAST_SYSTEM_WINDOW) {
    return WindowManagerImpl.ADD_OKAY;
  }
  String permission = null;
  switch (type) {
    case TYPE_TOAST:
      // XXX right now the app process has complete control over
      // this... should introduce a token to let the system
      // monitor/control what they are doing.
      break;
    case TYPE_INPUT_METHOD:
    case TYPE_WALLPAPER:
      // The window manager will check these.
      break;
    case TYPE_PHONE:
    case TYPE_PRIORITY_PHONE:
    case TYPE_SYSTEM_ALERT:
    case TYPE_SYSTEM_ERROR:
    case TYPE_SYSTEM_OVERLAY:
      permission = android.Manifest.permission.SYSTEM_ALERT_WINDOW;
      break;
    default:
      permission = android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
  }
  if (permission != null) {
    if (mContext.checkCallingOrSelfPermission(permission)
        != PackageManager.PERMISSION_GRANTED) {
      return WindowManagerImpl.ADD_PERMISSION_DENIED;
    }
  }
  return WindowManagerImpl.ADD_OKAY;
}

明顯不適合的 TypeTYPE_TOAST, TYPE_INPUT_METHOD, TYPE_WALLPAPER; 可能適合的 TypeTYPE_PHONE, TYPE_PRIORITY_PHONE, TYPE_SYSTEM_ALERT, TYPE_SYSTEM_ERROR, TYPE_SYSTEM_OVERLAY; 其它類型的 Type

需要系統(tǒng)簽名權(quán)限:

android.Manifest.permission.INTERNAL_SYSTEM_WINDOW

而申請該權(quán)限需要系統(tǒng)簽名,所以我們是無法獲取權(quán)限的。

TYPE_PHONE

/**
 * Window type: phone. These are non-application windows providing
 * user interaction with the phone (in particular incoming calls).
 * These windows are normally placed above all applications, but behind
 * the status bar.
 * In multiuser systems shows on all users' windows.
 */
public static final int TYPE_PHONE       = FIRST_SYSTEM_WINDOW+2;

TYPE_PHONE 類型的窗口可以顯示在其它 APP 的上面,但不能顯示在鎖屏的上面,所以 PASS。

TYPE_PRIORITY_PHONE

/**
 * Window type: priority phone UI, which needs to be displayed even if
 * the keyguard is active. These windows must not take input
 * focus, or they will interfere with the keyguard.
 * In multiuser systems shows on all users' windows.
 */
public static final int TYPE_PRIORITY_PHONE   = FIRST_SYSTEM_WINDOW+7;

TYPE_PRIORITY_PHONE 類型的窗口可以顯示在其它 APP 的上面,但不能顯示在鎖屏的上面,所以 PASS。而且實際的行為和注釋并不相符,該類型的窗口是可以獲取交互事件的,具體原因待查。

TYPE_SYSTEM_ALERT

/**
 * Window type: system window, such as low power alert. These windows
 * are always on top of application windows.
 * In multiuser systems shows only on the owning user's window.
 */
public static final int TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3;

TYPE_SYSTEM_ALERT 類型的窗口可以顯示在其它 APP 的上面,但不能顯示在鎖屏的上面,所以 PASS。

TYPE_SYSTEM_OVERLAY

/**
 * Window type: system overlay windows, which need to be displayed
 * on top of everything else. These windows must not take input
 * focus, or they will interfere with the keyguard.
 * In multiuser systems shows only on the owning user's window.
 */
public static final int TYPE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+6;

TYPE_SYSTEM_OVERLAY 類型的窗口可以顯示在所有其它窗口的上面,包括鎖屏,而且不會影響它下面窗口的交互事件響應(yīng),但是該屬性窗口不能獲得焦點,無法進(jìn)行交互(如果該窗口可以獲取焦點,那么就可以用來抓取用戶的鎖屏密碼,出于安全考慮,系統(tǒng)是不會允許的),所以只能用來簡單的展示內(nèi)容,如果需要交互的鎖屏彈窗,那么該屬性 PASS。

TYPE_SYSTEM_ERROR

/**
 * Window type: internal system error windows, appear on top of
 * everything they can.
 * In multiuser systems shows only on the owning user's window.
 */
public static final int TYPE_SYSTEM_ERROR = FIRST_SYSTEM_WINDOW+10;

在原生 ROM 5.1 下試驗是可以顯示出來的,但根據(jù)注釋來看(appear on top of everything they can)不是在所有情況下都可以顯示在鎖屏上面的,而且像 MIUI 和 Flyme 等 ROM 默認(rèn)是屏蔽浮窗權(quán)限的,考慮到這點,利用 WindowManager 添加浮窗的方式實現(xiàn)鎖屏彈窗的方案基本 PASS。

使用 Activity 的方式實現(xiàn)

首先需要對 Activity 進(jìn)行如下設(shè)置

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState); 
  final Window win = getWindow(); 
  win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
      | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
      | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 
      | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
}

其中最主要也是必須要設(shè)置的就是:FLAG_SHOW_WHEN_LOCKED,顧名思義就是鎖屏下顯示該 Activity。而其它幾個 Flag 包括:解鎖、保持屏幕常亮、點亮屏幕可以根據(jù)具體的需求選擇設(shè)置。

在 AndroidManifest.xml 中聲明 Activity

同樣該 Activity 也需要在 AndroidManifest.xml 中聲明,聲明時需注意添加 android:excludeFromRecents="true" 屬性,是為了將該 Activity 從最近任務(wù)列表中去除,否則用戶會覺得很奇怪。還有因為這個 Activity 會整個蓋在鎖屏上面,而且就算設(shè)置成背景透明,鎖屏界面也不會顯示在下面(系統(tǒng)主要是出于安全考慮),所以需要考慮下該 Activity 的背景,這里為了顯示不要太突兀將主題設(shè)為壁紙。

<activity android:name=".LockScreenActivity" 
     android:launchMode="singleInstance" 
     android:excludeFromRecents="true" 
     android:theme="@android:style/Theme.Wallpaper.NoTitleBar"/>

啟動 Activity

由于該 Activity 是為了在鎖屏的情況下顯示的,所以啟動 Activity 時不要忘了判斷手機(jī)是否處于鎖屏狀態(tài),可以通過下面這種方式判斷鎖屏狀態(tài):

KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (km.inKeyguardRestrictedInputMode()) {
  // 處于鎖屏狀態(tài)
}

總結(jié)

以上就是在Android中實現(xiàn)鎖屏狀態(tài)下彈窗效果的全部內(nèi)容,希望本文的內(nèi)容對大家開發(fā)Android的時候能有所幫助,如果有疑問歡迎大家留言討論。

相關(guān)文章

最新評論