How Android realizes the lock-screen state of the window _android

Source: Internet
Author: User

Objective

To achieve the window on the lock screen, the first idea is to WindowManager use Window settings Flag , by setting Flag the display priority to let the window display in the top of the lock screen.

The next step is to experiment with potentially relevant Window Type properties and verify that the scenario is feasible.

Before you try each of the Window Type attributes, you need to specify the permissions that each type requires, and com.android.internal.policy.impl.PhoneWindowManager.checkAddPermission the following source code:

public int checkaddpermission (Windowmanager.layoutparams attrs) {int type = Attrs.type; if (Type < WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW | | | type > WindowManager.LayoutParams.LAST_SYSTEM_WIN
  DOW) {return windowmanagerimpl.add_okay;
  } String permission = null; Switch (type) {case type_toast://XXX Right now the app process has complete control over//this ... sho
      Uld introduce a token to let the system//Monitor/control what they. are doing.
    Break
      Case Type_input_method:case Type_wallpaper://The window manager'll check these.
    Break Case Type_phone:case type_priority_phone:case type_system_alert:case type_system_error:case TYPE_SYSTE M_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; }

Obviously unsuitable Type : TYPE_TOAST , TYPE_INPUT_METHOD , TYPE_WALLPAPER ; May fit Type :,,, TYPE_PHONE TYPE_PRIORITY_PHONE TYPE_SYSTEM_ALERT TYPE_SYSTEM_ERROR , TYPE_SYSTEM_OVERLAY ; other types Type :

System signing permissions are required:

Android. Manifest.permission.INTERNAL_SYSTEM_WINDOW

The request for this permission requires a system signature, so we cannot get permission.

Type_phone

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

TYPE_PHONEThe type of window can be displayed on top of other apps, but not on the top of the lock screen, so pass.

Type_priority_phone

/**
 * Window type:priority phone UI, which needs to being displayed even if * The Keyguard is
 active. These windows must not take input
 * focus, or they'll interfere with the keyguard.
 * In multiuser systems shows to all users ' windows.
 * * Public
static final int Type_priority_phone   = first_system_window+7;

TYPE_PRIORITY_PHONEThe type of window can be displayed on top of other apps, but not on the top of the lock screen, so pass. and the actual behavior and the annotation does not match, this type of window can obtain the interactive event, the specific reason is not available.

Type_system_alert

/**
 * Window Type:system window, such as low power alert. These windows
 * are always on the 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 The type of window can be displayed on top of other apps, but not on the top of the lock screen, so pass.

Type_system_overlay

/**
 * Window type:system overlay windows, which need to is displayed * on top of
 everything else. These windows must not take input
 * focus, or they'll 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_OVERLAYThe type of window can be displayed on top of all other windows, includes a lock screen and does not affect the interactive event response of the window below it, but the property window does not have the focus and cannot interact (if the window can get the focus, it can be used to crawl the user's lock-screen password, which is not allowed for security reasons) , so it can only be used for simple display of content, if you need to interact with the lock-screen window, then this property 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;

Under the primary ROM 5.1 The experiment can be shown, but according to the comments (appear on the top of everything they can) is not in all cases can be displayed on the lock screen, and such as MIUI and FlyMe ROM default is to mask the floating window permissions , in view of this, using WindowManager the method of adding floating window to realize the scheme of the Lock-screen window is basically pass.

Using activity as a way to achieve

You first need to set the activity as follows

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

One of the most important things must be set is: FLAG_SHOW_WHEN_LOCKED , as the name implies is the lock screen to display the Activity . The other several Flag include: unlock, keep screen illuminated steady, light up the screen can be based on specific requirements to choose settings.

Declaring an activity in the Androidmanifest.xml

It Activity also needs to be AndroidManifest.xml declared in, and the declaration should pay attention to adding android:excludeFromRecents="true" attributes, in order to Activity remove the list of recent tasks, or users will find it very strange. And because this Activity will cover the entire lock screen, and even if set to the background transparent, the lock screen interface will not be shown below (the system is mainly for security reasons), so you need to consider the Activity background, here to show not too abrupt theme to the wallpaper.

<activity android:name= ". Lockscreenactivity " 
     android:launchmode=" singleinstance " 
     android:excludefromrecents=" true " 
     Android: Theme= "@android: Style/theme.wallpaper.notitlebar"/>

Start activity

Because this Activity is to display in the case of the lock screen, so Activity do not forget to start to determine whether the phone is in the lock screen state, you can use the following way to determine the lock screen state:

Keyguardmanager km = (keyguardmanager) context.getsystemservice (context.keyguard_service);
if (Km.inkeyguardrestrictedinputmode ()) {
  //In lock-screen state
}

Summarize

The above is in Android to achieve the lock screen under the window effect of all the content, I hope this article on the content of the development of Android when you can help, if there is doubt welcome to comment on the discussion.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.