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();
CodePudding user response:
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.
CodePudding user response:
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);