Android - Wake Up and Unlock Device

Android - Wake Up and Unlock Device

I solved the issue. The reason we observed different behaviour when the device was plugged in via USB was because the device's CPU was not going to sleep. I assume this is either a result of the debug mode setting, or simply how it behaves when plugged in to a computer since the power-saving feature of CPU-sleeping would be irrelevant. Obviously, when the device is not plugged in, the CPU would happily take a nap, and while we did observe the application randomly running (it would wake itself up at random times), the timing would be inconsistent. I further assume this is because the few CPU cycles that occurred are allocated sparingly, and our application would be given very few cycles at "random" times.

So our solution was to grab a partial wake lock when the device goes into the background (which is done in the onPause method), and release the lock in the onResume method. This seems to prevent the CPU from sleeping. We continue to use the full wake lock and keyguard disable to wake the device when we need to. Using the partial wake lock seems to keep the CPU from sleeping, and the device does appear to wake up properly when expected. Here is our updated code, in case anyone comes across this issue:

// Called from onCreate
protected void createWakeLocks(){
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
fullWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK");
partialWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Loneworker - PARTIAL WAKE LOCK");
}

// Called implicitly when device is about to sleep or application is backgrounded
protected void onPause(){
super.onPause();
partialWakeLock.acquire();
}

// Called implicitly when device is about to wake up or foregrounded
protected void onResume(){
super.onResume();
if(fullWakeLock.isHeld()){
fullWakeLock.release();
}
if(partialWakeLock.isHeld()){
partialWakeLock.release();
}
}

// Called whenever we need to wake up the device
public void wakeDevice() {
fullWakeLock.acquire();

KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
}

Android: Wake & unlock phone

Use this code below in your service.

PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "YourServie");
mWakeLock.acquire();
[...]
mWakeLock.release();

If you want to unlock the screen as well, register a receiver in your service that monitors if the screen is turned on/off and if it is turned off and you want to unlock the phone, start an activity with this code in onCreate:

Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
this.finish();
return;

I know, this is a rather dirty, but as far as I know, there is no other way of unlocking the lockscreen (and this will only work if there are no passwords etc set, so it must be the normal "slide to unlock" screen).

And don't forget to add android.permission.WAKE_LOCK ;-)

/edit: I just saw you are already using an Activity. If you have one and don't need the service at all, just put this code into the activity.

Wake and unlock Android phone screen when compile and run project?

Better and easiest solution is use the option (in development section) that the screen never goes off I thing the name is "Stay awake". This option prevent your phone to get locked when the usb cable is connected.

Wake up device programmatically

Ok i found it. I just add that piece of code and it worked

KeyguardManager manager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock lock = manager.newKeyguardLock("abc");
lock.disableKeyguard();

How wake up device and show an activity on top of lock screen for alarm?

Just have to set the correct window flags. This works from API 14(?) up to Oreo Beta:

Activity:

[Activity(Label = "AlarmActivity")]
public class AlarmActivity : Activity, View.IOnClickListener
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Alarm);
FindViewById<Button>(Resource.Id.myButton).SetOnClickListener(this);
}

public override void OnAttachedToWindow()
{
Window.AddFlags(WindowManagerFlags.ShowWhenLocked |
WindowManagerFlags.KeepScreenOn |
WindowManagerFlags.DismissKeyguard |
WindowManagerFlags.TurnScreenOn);
}

public void OnClick(View v)
{
Finish();
}
}

AlarmManager Test:

Call this routine and the alarm manager will start up the AlarmActivity in one minute, so apply this within a Button click/listener and lock your screen:

using (var manager = (Android.App.AlarmManager)GetSystemService(AlarmService))
using (var calendar = Calendar.Instance)
{
calendar.Add(CalendarField.Minute, 1);
Log.Debug("SO", $"Current date is : {Calendar.Instance.Time.ToString()}");
Log.Debug("SO", $"Alarm will fire at {calendar.Time.ToString()}");
var alarmIntent = new Intent(ApplicationContext, typeof(AlarmActivity));
var pendingIntent = PendingIntent.GetActivity(this, 0, alarmIntent, PendingIntentFlags.OneShot);
manager.SetExact(AlarmType.RtcWakeup, calendar.TimeInMillis, pendingIntent);
}

Wake phone from unlock screen android

If you are simply looking for MainActivity to show when the phone is locked, there are Window flags that can be set in onCreate() to make that happen.

FLAG_SHOW_WHEN_LOCKED will enable your activity to be visible to the user when the phone is locked, and FLAG_DISMISS_KEYGUARD will unlock the phone IF the user has not configured a secure (pin/pattern/face/etc) keyguard.

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


Related Topics



Leave a reply



Submit