2

I have broadcastreceiver in my application and it's work good, when i connect with the smart phone to bluetooth device it's show me a alert dialog. But, if the screen is off or in the lock screen, it's not show me the alert dialog, and i want to show the alert dialog even if the smartphone on the lock screen or the screen off. How i can do this?

Thanks!

tornal vergaro
  • 157
  • 1
  • 1
  • 13
  • i am facing same problem . Did you got any solution for this? – Parag Chauhan Aug 28 '13 at 12:14
  • See if my answer is helpful. Anyway, if you want to receive any help, next time you should provide at least part of the code in question. – Paolo Rovelli Mar 24 '14 at 17:51
  • I have to use another activity instead of alert dialog. See my answer at this post if you want to bring that activity at front even if when screen is locked http://stackoverflow.com/questions/4352548/how-to-unlock-the-screen-when-broadcastreceiver-is-called/31726130#31726130 – Junaid Aug 02 '15 at 14:18

2 Answers2

2
public class ScreenReceiver extends BroadcastReceiver {


    public static boolean wasScreenOn = true;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
           //do something here

        }
    }
}

This is how you register the receiver.

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
mihirjoshi
  • 12,161
  • 7
  • 47
  • 78
  • just make class `ScreenReceiver` and apply `registerReceiver` code in `onCreate()`. – mihirjoshi Jul 28 '13 at 13:26
  • it's not working, i make the class ScreenReceiver and i add all the registerReceiver (from IntentFilter filter = .... to filter); ) code to my onCreate(), and it's not open my screen and show my dialog... – tornal vergaro Jul 28 '13 at 13:31
  • In the my second activity, i have 2 acticities, one is the main and the second come up when the Receiver come up. (when i connect a blootooth, the second acticity come up). – tornal vergaro Jul 28 '13 at 14:39
0

Since you didn't provide any code, I cannot be sure about this. However, your problem might be related to the context from which you are declaring the Intent (if any), or anyway the dialog. Try to use context.getApplicationContext() instead of context / this:

final Intent dialogIntent = new Intent(context.getApplicationContext(), DialogActivity.class);
context.startActivity(dialogIntent);
Paolo Rovelli
  • 9,396
  • 2
  • 58
  • 37