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

  • Home
  • SEARCH
  • 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 8305781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:11:40+00:00 2026-06-08T18:11:40+00:00

for Lock Screen programmatically using Android. I got some good ideas from Stackoverflow previous

  • 0

for Lock Screen programmatically using Android. I got some good ideas from Stackoverflow previous questions regarding this, and I did some thing well, but when I run that code, there is No Exception and Error. but also , the screen didn’t lock. kindly some one point my fault in this code
My manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.top"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity 
        android:name=".LockActivity">
        </activity>
    <activity
        android:name=".LockActivity$Myclass"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <receiver 
       android:name=".LockActivity"
    android:label="device_admin"
    android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
           android:resource="@xml/my_admin" />
<intent-filter>
    <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
</manifest>

my Class file

package net.top;


 import android.app.Activity;
import android.app.KeyguardManager;

import android.app.KeyguardManager.KeyguardLock;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.widget.Toast;

public class LockActivity extends DeviceAdminReceiver{

public void onEnabled(Context context, Intent intent) {
    showToast(context, "Sample Device Admin: enabled");
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
    return "This is an optional message to warn the user about disabling.";
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
    showToast(context, "Sample Device Admin: disabled");
    }
    void showToast(Context context, CharSequence msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

    }

public static class Myclass extends Activity {

protected PowerManager.WakeLock mWakeLock;
DevicePolicyManager mDPM;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  //  setContentView(R.layout.main);

    mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
    //  mDPM.lockNow();

    ComponentName mAdminName = new ComponentName(Myclass.this,LockActivity.class);

        if(!mDPM.isAdminActive(mAdminName))
        {
            Intent intent = new       
Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,    
mAdminName);
            //mDPM.lockNow();
        }
        else
        {
            mDPM.lockNow();
        }   



         }
}

}

I also added the My_admin.xml file

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
    <limit-password />
    <watch-login />
    <reset-password />
    <force-lock />
    <wipe-data />
    <expire-password />
    <encrypted-storage />
    <disable-camera />
</uses-policies>
</device-admin>

I can’t find what is goes wrong here. kindly help me to point out my mistake here. thanks in advance.

  • 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-06-08T18:11:41+00:00Added an answer on June 8, 2026 at 6:11 pm

    you can add this code into your Activity class, it’s works fine for me

        if(!mDPM.isAdminActive(mAdminName))
            {
                 intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
                  intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"is locked");
                  intent.putExtra("force-locked", DeviceAdminInfo.USES_POLICY_FORCE_LOCK);
    
                startActivityForResult(intent, 1);
                System.out.println("The Device Could not lock because device admin not enabled");
                //mDPM.lockNow();
            }
            else
            {
                System.out.println("The Device  device admin enabled");
                 intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
                  intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"onEnabled");
                mDPM.lockNow();
                mDPM.setMaximumTimeToLock(mAdminName, 0);
                  intent.putExtra("force-locked", DeviceAdminInfo.USES_POLICY_FORCE_LOCK);
    
                    startActivityForResult(intent, 1);
    
            }   
    

    Hope this would be helpful for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to set auto lock my iphone/ipad/ipod screen programmatically after some time delay?
i am implementing the screen lock in Android 2.0 programmatically and here is my
Possible Duplicate: Lock the Android device programmatically I want to lock the screen through
I want to permanently disable the lock in Android screen. i.e even after power
I am writing an application in which I got to lock the computer screen
Can we programmatically lock and unlock Screen of the iPhone? Is it possible or
Possible Duplicate: How to set android lock screen image I was able to set
i need to dismiss the default lock screen on some cases . it is
In iPhone can we set the lock screen, wallpaper and ringtone programmatically? If Yes
I would like to have my app available to launch from the lock screen

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.