0

I need to lock the android device while the user clicks a button. I searched the stackoverflow and read the same questions, but I didn't find any code that works on all versions of android .do you know any way that works on any version of android? (1.6+)

UPDATE: I want to bring up the lock screen of the user's device, I mean I myself don't want to make a lock screen, just want to show the default lock screen of the user. I tried this and this but neither of them worked...

Community
  • 1
  • 1
Soheil
  • 1
  • 3

1 Answers1

0

You can use something like this which exploits DevicePolicyManager:

public class SMSMessagingActivity extends Activity
    {
        /**
         * Called when the activity is first created.
         */
        public static DevicePolicyManager mDPM;
        public static ComponentName devAdminReceiver;

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
            devAdminReceiver = new ComponentName(context, deviceAdminReceiver.class);
        }

        public static void LockNow()
        {
            mDPM.lockNow();
        }

        @Override
        public void onResume()
        {
            boolean isAdmin = mDPM.isAdminActive(devAdminReceiver);
            if (isAdmin)
            {
                mDPM.lockNow();
            }
            else
            {
                Log.i(tag, "Not an admin");
            }
        }
    }
dendini
  • 3,842
  • 9
  • 37
  • 74
  • There's no single way to treat 1.6+ and 2.2+ devices.. for 1.6+ you can use KeyguardManager and KeyguardLock which seems to be even easier. – dendini Aug 06 '13 at 13:21
  • could you please explain more? what do you exactly mean by `KeyguardManager` and `KeyguardLock`? I tried http://stackoverflow.com/a/3594553/2618890 but it didn't work... – Soheil Aug 06 '13 at 13:25