-1

Here is the piece of code that creates my alert dialog:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Battery level less than 20%:"+al)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {
                            //do things
                        }
}); 

AlertDialog alert = builder.create();
alert.show();
Strider
  • 4,452
  • 3
  • 24
  • 35
M.S
  • 11
  • 5
  • You should check if your app is running in the background and if so show an dialogbox. This given answer might help you: http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background – Bas Jun 08 '15 at 09:02
  • [Here](http://stackoverflow.com/questions/3291655/get-battery-level-and-state-in-android) is a good example of what you need. – Strider Jun 08 '15 at 09:27

1 Answers1

1

Since alert dialog depends on the context, you actually can't display it while your application is running in background. However you can use status bar notification which will allow you to notify the user about something while the app is running in the background. Here is a good tutorial will help you to accomplish that: http://www.learn2crack.com/2013/12/android-status-bar-notification.html Hope this will help.

Anurag Singh
  • 156
  • 2
  • 14
Ahmad Sanie
  • 3,678
  • 2
  • 21
  • 56