-2

i want to create app in that when i click on my app icon , screen autmatically locked, please any one help me to how to do this ?

I much googling but not find like this, so please help me to do it.

In this i click on app_launcher icon and directly mobile screen locked.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Nirav Mehta
  • 1,715
  • 4
  • 23
  • 42

2 Answers2

1

Try this code (lock screen and screen off):

public DevicePolicyManager deviceAdminPolicyManager = (DevicePolicyManager)getSystemService( Context.DEVICE_POLICY_SERVICE);
public ComponentName deviceAdminComponentName = new ComponentName( this, DeviceAdmin.class);

deviceAdminPolicyManager.lockNow();
PowerManager powerManager = (PowerManager) getSystemService( Context.POWER_SERVICE);
if( powerManager.isScreenOn()) powerManager.goToSleep( System.currentTimeMillis() + 1000L);

Your app must be added as device administrator by Settings - Security - Device administartors.

Also add in Manifest:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.DEVICE_POWER"/> <!-- for screen off and on -->
<uses-permission android:name="android.permission.WAKE_LOCK"/>

 <receiver android:label="@string/label" android:name="com.package$DeviceAdmin" android:permission="android.permission.BIND_DEVICE_ADMIN">
  <meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin" />
  <intent-filter>
   <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
  </intent-filter>
 </receiver>

And:

public  static class DeviceAdmin extends DeviceAdminReceiver
 {
   public CharSequence onDisableRequested( Context paramContext, Intent paramIntent) { return ""; }
   public void onDisabled( Context paramContext, Intent paramIntent) {}
   public void onEnabled( Context paramContext, Intent paramIntent) {}
   public void onPasswordChanged( Context paramContext, Intent paramIntent) {}
 }

DeviceAdmin.xml

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
 <uses-policies>
  <force-lock />
 </uses-policies>
</device-admin>
Tapa Save
  • 4,769
  • 5
  • 32
  • 54
  • what is into the **device_admin** xml file and permission **** is only allowed to system apps – Nirav Mehta Sep 30 '14 at 06:29
  • I add deviceadmin xml file code. Try start app with DEVICE_POWER permission ignoring warning. Not forget add your app in Device Admin – Tapa Save Sep 30 '14 at 06:33
  • there any thing to write in onDisabled() and onEnable() in DeviceAdmin class ? – Nirav Mehta Sep 30 '14 at 07:25
  • i got answes using your answers thank you very much +1, i added some code in your ans – Nirav Mehta Sep 30 '14 at 10:17
  • my added code in if : DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName componentName = new ComponentName(this, DeviceAdmin.class); boolean active = devicePolicyManager.isAdminActive(componentName); if (active) { devicePolicyManager.lockNow(); PowerManager powerManager = (PowerManager) getSystemService( Context.POWER_SERVICE); if( powerManager.isScreenOn()) powerManager.goToSleep( System.currentTimeMillis() + 1000L); Toast.makeText(getApplicationContext(), "active", Toast.LENGTH_SHORT).show(); } – Nirav Mehta Sep 30 '14 at 10:28
  • else : else { Intent adminIntent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); adminIntent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); startActivity(adminIntent); } finish(); – Nirav Mehta Sep 30 '14 at 10:29
  • may be my added code help to other person. – Nirav Mehta Sep 30 '14 at 10:30
0
//For Unlock  

WindowManager winManager = Context.getSystemService(Context.WINDOW_SERVICE);    
Window window = getWindow();      
window.addFlags(winManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 

OR

    KeyguardManager km = (KeyguardManager) context
     .getSystemService(Context.KEYGUARD_SERVICE);
   final KeyguardManager.KeyguardLock kl = km
     .newKeyguardLock("MyKeyguardLock");
   kl.disableKeyguard();

   PowerManager pm = (PowerManager) context
     .getSystemService(Context.POWER_SERVICE);
   WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
     | PowerManager.ACQUIRE_CAUSES_WAKEUP
     | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
   wakeLock.acquire(); 


//Lock device

DevicePolicyManager myDPM;    
myDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
Vaishali Sutariya
  • 5,093
  • 30
  • 32