Next, I have a BroadCast
and I want it when it runs the phone screen to be turned on!
Probably the phone will be in sleep, so I want it to turn on the screen of the device!
I think it's simple, but I'm not finding a solution!
Next, I have a BroadCast
and I want it when it runs the phone screen to be turned on!
Probably the phone will be in sleep, so I want it to turn on the screen of the device!
I think it's simple, but I'm not finding a solution!
The solution is BroadcastReceiver
to call a Activity
which when displayed will cause the screen to be turned on and, if necessary, unlocked ). This is done as follows:
public class MeuReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent intentParaIniciarAtividade = new Intent(context, MinhaAtividade.class);
intentParaIniciarAtividade.putBoolean(MinhaAtividade.EXTRA_LIGAR_E_DESTRAVAR_TELA, true);
context.startActivity(intentParaIniciarAtividade);
}
}
My Activity.java:
public class MinhaAtividade extends Activity {
public static final String EXTRA_LIGAR_E_DESTRAVAR_TELA = MinhaAtividade.class.getPackage().getName() + ".LIGAR_E_DESTRAVAR_TELA";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_minha_atividade);
if (true == getIntent().getBoolean(EXTRA_LIGAR_E_DESTRAVAR_TELA, false)) {
// Combinando duas flags: FLAG_TURN_SCREEN_ON e FLAG_DISMISS_KEYGUARD.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
}
}
Flags that can be combined in this case depending on the situation:
To unlock the screen: link
To display the Activity in front of the locked screen but without removing the lock: link
To turn on the screen illumination: link
To keep the screen on: link
Important note: For flags to work, the window that Activity
displays must be a window that occupies full screen ( full screen ) top of the other activities.
( Information extracted from this answer in SOen ).
The intent that is triggered by android is ACTION_SCREEN_ON .
However, you can not register an intent filter in your manifest for this Intent. You need to register programmatically using the registerReceiver () method.
Just making a fix, to keep the screen switched on you would use the flag FLAG_ALLOW_LOCK_WHILE_SCREEN_ON