Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7664049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:12:18+00:00 2026-05-31T14:12:18+00:00

Recently I am doing one project in emulator lock and unlock screen. I put

  • 0

Recently I am doing one project in emulator lock and unlock screen. I put one button. I want to do that if i press that button I want to lock the phone.

my problem is when i press the button nothing is happening.
please give me any idea about this.

how i get lock unlock perform using this code.

     public class Test extends Activity implements OnClickListener{
/** Called when the activity is first created. */ 
Button btn1;
private KeyguardManager mKeyguardManager;
private KeyguardManager.KeyguardLock mKeyguardLock;
private static final String TAG = "ALERTLock";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btn1=(Button)findViewById(R.id.button1);
    btn1.setOnClickListener(this);

        int flags = getFlagsForVersion();        
        getWindow().addFlags(flags);
        mKeyguardManager = (KeyguardManager)     

            getSystemService(Context.KEYGUARD_SERVICE);   
}


public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v==btn1)
    {           
        disableKeyguard();

    }
        else    
         {
            enableKeyguard();
         }
}
private int getFlagsForVersion() {
        final String possibleFlags[] = new String[] {
            "FLAG_SHOW_WHEN_LOCKED",
            "FLAG_DISMISS_KEYGUARD",
            "FLAG_TURN_SCREEN_ON"
        };

        int flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        for(String flag:possibleFlags) {
            try {
                Field field = WindowManager.LayoutParams.class.getField(flag);
                int value = field.getInt(null);
                flags |= value;
            }
            catch(NoSuchFieldException e) { }
            catch(IllegalAccessException e) { }
        }

        return flags;
    }

    private synchronized void enableKeyguard() {
        if (mKeyguardLock != null) {
            mKeyguardLock.reenableKeyguard();
            mKeyguardLock = null;
        }
    }

    private synchronized void disableKeyguard() {
        if (mKeyguardLock == null) {
            mKeyguardLock = mKeyguardManager.newKeyguardLock(TAG);
            mKeyguardLock.disableKeyguard();
        }
    }    
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        disableKeyguard();
    }

    @Override
    public void onResume() {
        super.onResume();
        disableKeyguard();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:12:20+00:00Added an answer on May 31, 2026 at 2:12 pm

    //try this once
    and check the logcat is printing the line enable

    public void onClick(View v) {
    
    switch(v.getId()){   
      case R.id.button1: 
        //do this
    Log.d("clicked enable","-----");
    enableKeyguard();
    
        break;  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a problem recently with a php project that I finally realised was
One thing I've started doing more often recently is retrieving some data at the
Recently I started doing a C++ project, and started it using the internal building
Recently I've been doing quite a big project with php + mysql. And now
I've been doing a project at work recently focused on an almost entirely client-driven
I've recently run into a problem with my Django project and memory usage on
Summary I recently had a conversation with the creator of a framework that one
I am new to Django. Recently I am doing a project from my peer.
I recently took over a project that was left stagnate a team member quit
Problem I've recently started using Linux on one of my laptops. I installed XAMPP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.