Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wake up android and show the activity?

Please, help me. I have a broadcast reciever:

public class BrcRec extends BroadcastReceiver{
public static WakeLock wakeLock;
@Override
public void onReceive(Context context, Intent intent) {


    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
    wakeLock.acquire();
    //Осуществляем блокировку
    KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); 
    KeyguardLock keyguardLock =  keyguardManager.newKeyguardLock("TAG");
    keyguardLock.disableKeyguard();


    //Здесь можно делать обработку.
    Bundle extras = intent.getExtras();
    StringBuilder msgStr = new StringBuilder();

    msgStr.append("Одноразовый будильник: ");
    Format formatter = new SimpleDateFormat("hh:mm:ss a");
    msgStr.append(formatter.format(new Date()));
    // Creating activity must be there, i think
    Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();
    //Разблокируем поток.
    wakeLock.release();
}

And then it is in work, my android do not wake up: button blink one time and that is all. Where is a mistake?

I want to wake up android and call some activity in result.. Thank you.

like image 602
user2254276 Avatar asked Apr 07 '13 11:04

user2254276


2 Answers

In the Activity that you want to show can you add these flags:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

That will cause the Activity to wake the device.

like image 189
joelreeves Avatar answered Sep 27 '22 21:09

joelreeves


It is worth noting that what "joelreeves" wrote works even without using the PowerManager and Wakelock APIs. By simply adding the Flags on the onCreate of the activity, whenever it starts, it will fully remove the Keyguard and Lock from the phone.

like image 39
Bonatti Avatar answered Sep 27 '22 21:09

Bonatti