1

i develop such apps,i want to locked screen automatically through code on specific time choose by user through dialog box.

Please help me i don't idea about it. when user click on dialog text of time interval,dialog close and screen locked automatically on specific time choose by user,after inactivity.

2 Answers2

0

Define in your activity :

private DevicePolicyManager mDevicePolicyManager;
private ComponentName mComponentName;

    mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    mComponentName = new ComponentName(this, MyAdminReceiver.class); 

register and unregister click on main activity :

txtRegister.setOnClickListener(new OnClickListener() {
                //
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(
                            DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                            mComponentName);
                    intent.putExtra(
                            DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                            description);
                    startActivityForResult(intent, ADMIN_INTENT);

                }
            });
            txtUnRegister.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {


                    mComponentName = new ComponentName(MainActivity.this, MyAdminReceiver.class);
                    mDevicePolicyManager.removeActiveAdmin(mComponentName);
                    Toast.makeText(getApplicationContext(),
                            "Admin registration removed",
                            Toast.LENGTH_SHORT).show();

                }
            });     

create admin class MyAdminReceiver:

import android.app.admin.DeviceAdminReceiver;

public class MyAdminReceiver extends DeviceAdminReceiver{

}      

add in your menifist :

<receiver
        android:name="MyAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@layout/admin" />

        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
</receiver>  

create one xml file named admin.xml :

<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-policies>
    <force-lock />
</uses-policies>

write follwowing code where you want to perform your action (it your click also) in your activity :

boolean isAdmin = mDevicePolicyManager.isAdminActive(mComponentName);
    if (isAdmin) {
        Handler mHandler = new Handler();

        mHandler.post(new Runnable() {

            @Override
            public void run() {

                final Handler handler = new Handler();
                Timer t = new Timer();
                t.schedule(new TimerTask() {
                    public void run() {
                        handler.post(new Runnable() {
                            public void run() {
                                mDevicePolicyManager.lockNow();
                            }
                        });
                    }
                }, delay); // flashing of screen that you want to time delay
            }
        });

    } else {
        Toast.makeText(getApplicationContext(), "first register as admin",
                Toast.LENGTH_SHORT).show();
    }                                                                      
Nirav Mehta
  • 1,715
  • 4
  • 23
  • 42
  • greats it is a perfect solutions for me.. thank you very much i success 80% due to your help,again thanx mr. @niravmehta –  May 29 '14 at 05:04
  • using this code when i first time register its work fine, then i choose spefic time from alert box, then also ok but when i unregister as admin then i immediately click on different interval but we check also if is admin then it goest to lock but some times it not admin then also goes to that function,means some times it returns true if admin is unregister..so it gives below error `No active admin owned by uid 10102 for policy #3` otherwise it works fine have any solution of it ?? –  May 29 '14 at 05:42
  • Note: If you try to call the Intent for Admin Device other that from an Activity subclass there are chances you might get an error to use Intent.FLAG_ACTIVITY_NEW_TASK but when you use that your window might not pop ,Also you cannot un-install your app unless it has not be unregistered as an admin – Nirav Mehta May 29 '14 at 05:57
  • it work fine when activity open but when this apps close then after screen can not be lock so after apps close then whatever time period that we choose at that time how screen lock/off/sleep ?? ans my second comment problem remian. –  May 29 '14 at 06:06
0

I giving you a another best way to screen off on specific time as below :

declare in your activity :

 int defTimeOut = 0;

write follwing code in your activity in your specific time interval click :

    final int DELAY = 60000; // 1 minute you change here as your time interval click
    defTimeOut = Settings.System.getInt(getContentResolver(),
            Settings.System.SCREEN_OFF_TIMEOUT, DELAY);
    Settings.System.putInt(getContentResolver(),
            Settings.System.SCREEN_OFF_TIMEOUT, DELAY);

in destroy in your activity :

@Override
protected void onDestroy() 
{
    super.onDestroy();
    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, defTimeOut);
}

Take following permission in your menifest file :

   <uses-permission android:name="android.permission.WRITE_SETTINGS" />
Nirav Mehta
  • 1,715
  • 4
  • 23
  • 42
  • great way to off screen on specific time , superb thank you very much.it is very appreciated.i do this using your help your both code for off screen and lock as below both are appreciated and working. –  May 29 '14 at 10:26