How to lock/unlock phone programmatically : Android

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

In my application, I need to lock and unlock phone. I searched for it, I found many answers, I tried them but no one is working.

I need to unlock phone on onCreate() and lock phone again while I'm finishing my activity.

Do you guys know any method to do the same?

Thanks friends.

EDIT:

Some links, that I have tried are:

How to display Activity when the screen is locked?

Android screen lock/ unlock programmatically

https://groups.google.com/forum/#!topic/android-developers/BOLjJTMO4zE

In my application I'm scheduling a task using AlarmManager and then enabling the phone lock. My activity is getting started on scheduled time but it not unlocking my phone. when I'm unlocking my phone manually running activity is getting appeared

回答1:

on BroadcastReceiver set up the wakelock and in the activity

Do This:

Window window = this.getWindow(); window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD); window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED); window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON); 

import following

import android.view.Window; import android.view.WindowManager.LayoutParams; 

it solved my problem ,rate it up:-)))



回答2:

DevicePolicyManager deviceManger; ActivityManager activityManager; ComponentName compName; 

add code in onCreate()

deviceManger = (DevicePolicyManager)getSystemService(             Context.DEVICE_POLICY_SERVICE); activityManager = (ActivityManager)getSystemService(             Context.ACTIVITY_SERVICE); compName = new ComponentName(this, DeviceAdminLock.class); 

add code in manifest file

Create an xml resource file in res/xml as device_admin_sample.xml and add the following -

Add permissions in manifest file -

Create one receiver class which extends DeviceAdminReceiver class.

public class DeviceAdminLock extends DeviceAdminReceiver {  @Override public void onEnabled(Context context, Intent intent) { }  @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) { }  @Override public void onPasswordChanged(Context context, Intent intent) { }  @Override public void onPasswordFailed(Context context, Intent intent) { }  @Override public void onPasswordSucceeded(Context context, Intent intent) { } 

}

ask activate lock permission in activity class

Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,compName); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"Additional text explaining why this needs to be added."); startActivityForResult(intent, 1); 

check permission is given or not

boolean active = deviceManger.isAdminActive(compName); if (active) { // if available then lock   deviceManger.lockNow(); } 

unlock device

deviceManger.removeActiveAdmin(compName); 

To Unlock

KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);  final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");  kl.disableKeyguard();   PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);  WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK                                  | PowerManager.ACQUIRE_CAUSES_WAKEUP                                  | PowerManager.ON_AFTER_RELEASE, "MyWakeLock"); wakeLock.acquire(); 


回答3:

As of API 27, FLAG_SHOW_WHEN_LOCKED and FLAG_TURN_SCREEN_ON were deprecated. Instead use this in the Manifest

Or, for general usage, in your activity:

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {         setTurnScreenOn(true);         setShowWhenLocked(true);     } else {         window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);         window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);     } } 


回答4:

Try this...

@Override         protected void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);             getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);                     setContentView(R.layout.activity_main);     }      @Override         protected void onPause() {             super.onPause();             getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);           } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!