[Android] Lock screen, android lock screen

Source: Internet
Author: User

[Android] Lock screen, android lock screen

Recently, I have been playing with a lot of fun and have not written many things. Today, I will simply add a simple article. This is a design recently involved in locking the screen of an Android device. It is probably through a button or service message to control that the device interface is completely locked and you cannot click anything. It seems that this requirement may involve a lot of things, but it does not exist... The content is very simple, and it can be quickly followed by the Code.

Address: http://www.cnblogs.com/rossoneri/p/4409691.html

Put a button on the main interface to trigger the event:

 

Press the button to display another screen lock page and add an unlock button (if not, how can I exit the program --!)

As you can see, the status bar on the top of the screen is completely removed after the screen lock is enabled, and the Navigation bar button below fades down to a small dot. At this time, the program will not respond any time you click anywhere on the screen. Okay, the code below.

 

LockScreen. java:

package com.example.wow.demo_lockscreen;import android.content.Context;import android.graphics.PixelFormat;import android.graphics.Point;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.Button;/** * Created by wow on 15-4-9. */public class LockScreen {    Point mLpSize;    Button mBtnUnlock;    ViewGroup mView;    final WindowManager mWindowManager;    final WindowManager.LayoutParams mLp;    public LockScreen(Context mContext){        mView = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.lock_screen_view, null);        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);        mLp = new WindowManager.LayoutParams();        mBtnUnlock = (Button)mView.findViewById(R.id.btn_unlock);        mBtnUnlock.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                show(false);            }        });        initLp();    }    private void initLp(){        mLp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;// TYPE_SYSTEM_ALERT;        mLp.format = PixelFormat.RGBA_8888;        mLp.flags |= WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED                | WindowManager.LayoutParams.FLAG_FULLSCREEN                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;        mLp.systemUiVisibility = View.STATUS_BAR_HIDDEN;        mLp.width = WindowManager.LayoutParams.MATCH_PARENT;        mLp.height = WindowManager.LayoutParams.MATCH_PARENT;    }    public void show(boolean flag){        if (flag){            mWindowManager.addView(mView, mLp);        } else {            mWindowManager.removeView(mView);        }    }}

The focus here is

mLp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;

The type must be set to this type. The function of this type is to make the currently displayed view appear at the top of the table with no reason. Flags can be set as needed. You can add other content or delete some content without affecting it.

Here, you must add permissions to Manifest.

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

 

MainActivity:

package com.example.wow.demo_lockscreen;import android.app.Activity;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {    Button mBtnLock;    LockScreen mLockScreen;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mBtnLock = (Button)findViewById(R.id.btn_lock);        mLockScreen = new LockScreen(this);        mBtnLock.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                mLockScreen.show(true);            }        });    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

It is too simple to paste the interface file .. Oh, that's why I tested 5.0 perfectly. 4.2 is a bit of a problem and I don't know why .. Let me know something.

Well, the screen lock is actually that simple. Later, I will study some Interface Effects and make some practical screen lock functions.

I recently studied system bar, which is quite simple. I wrote several related blogs and found that my previous understanding is still a little biased. I plan to write a final version for a while, so I can get this knowledge done.

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.