Connect android screen

Asked

Viewed 1,027 times

1

Next, I have a BroadCast and I want when it runs the screen of the mobile phone to be turned on! The phone will probably be in Sleep, so I want him to turn on the screen of the device! I think it’s simple, but I’m not finding a solution!

3 answers

3


The solution is the BroadcastReceiver call a Activity which when displayed will cause the screen to be switched on and, if necessary, unlocked (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: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD

To display the Activity in front of the locked screen but without removing the lock: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED

To turn on the screen lighting: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_TURN_SCREEN_ON

To keep the screen on: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON

Important remark: For flags to work, the window that the Activity displays should be a window that occupies the whole screen (full screen) and find yourself at the top of other activities.

(Extracted information of that reply in Soen).

0

The Intent that is fired by android is ACTION_SCREEN_ON.

However, you cannot register an Intent filter in your manifest for this Intent. You need to register programmatically using the registerReceiver method().

  • OP does not care when the screen will turn on, he wants to turn on the screen when receiving a broadcast.

0

  • Friends! My service runs the FLAG_TURN_SCREEN_ON from time to time, but it only turns on the screen in the first cycle. There is solution for this?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.