1

I have an app that use the gps in the background, and in some situations I want to show an alert when the app is in the background: https://github.com/Tapadoo/Alerter

and works fine in the foreground but doesnt work in the background, how can i show that in background? here is my code:

                    Alerter.create(MainActivity.this)
                        .setTitle("Alert Title")
                        .setText("Alert text...")
                        .show();

3 Answers3

0

To deal with anything related to UIs, use Handler.

Option 1

new Handler(Looper.getMainLooper()).post(new Runnable() {
  public void run() {
    Alerter.create(MainActivity.this)
    .setTitle("Alert Title")
    .setText("Alert text...")
    .show();
  }
});

Option 2

new Handler(Looper.getMainLooper(), new Handler.Callback() {
  public boolean handleMessage(Message msg) {
   Alerter.create(MainActivity.this)
    .setTitle("Alert Title")
    .setText("Alert text...")
    .show();
    return true;
  }
}).sendEmptyMessage(0);
Darkman
  • 2,941
  • 2
  • 9
  • 14
0

You need the app to be on the foreground to access the Main Thread to display an AlertDialog on the UI. When your app is in the background it cannot access the UI but you can use the Handler suggested above by @Darkman.

You can also launch a notification instead.

Tonnie
  • 4,865
  • 3
  • 34
  • 50
0

You can use local notification instead of using alert dialog.Please try below link for more information related to notifications.

Local Notifications in Android?

Sumit Kumar
  • 263
  • 1
  • 6