简体   繁体   中英

Wake up screen and show dialog on Lock Screen

I have an alarm scheduled. Using WakeLock.acquire() and KeyguardLock.disableKeyguard() , I am able to wake the screen up and show my activity. However, I'd prefer to just show a dialog. The built in alarm on my HTC and Samsung devices act this way. I expected to find a method on the KeyguardLock object, but I didn't see anything in the docs that led me in this direction. How can I keep the KeyguardLock on, but show my dialog on it the way the built-in alarms do. Here is my current code that runs in onCreate()

    KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    keyguardLock = km.newKeyguardLock(KeyguardLockTag);
    keyguardLock.disableKeyguard();

    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    int flags = PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP;
    wakeLock = pm.newWakeLock(flags, WakeLockTag);
    wakeLock.acquire();

Just try this to wake your device screen even at lock.

package android.taskscheduler;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

public abstract class WakeIntentService extends IntentService
{
abstract void doReminderWork(Intent intent);
public static final String LOCK_NAME_STATIC = "your_package_name";
private static PowerManager.WakeLock lockStatic = null;

public static void acquireStaticLock(Context context){
   getLock(context).acquire();
}

synchronized private static PowerManager.WakeLock getLock(Context context)
{
   if(lockStatic == null)
       {
           PowerManager powManager = (PowerManager)     context.getSystemService(Context.POWER_SERVICE);

   lockStatic = powManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOCK_NAME_STATIC);
   lockStatic.setReferenceCounted(true);
   }
   return (lockStatic);
}
public WakeIntentService(String name) 
{
   super(name);
}
@Override
        final protected void onHandleIntent(Intent intent)
{
       try{
            doReminderWork(intent);

        }finally
   {
       getLock(this).release();

       }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM