It took me a while to figure this out. I have an app that I coded and it runs a service in the background. Given a specific event, I want to show an alert on the screen. How to do it?

All the ways I tried would work nicely, but the alert would only pop up when I opened the activity that started the service in the background. My goal was to have a “global alert” that will show up on the screen right then and there, no matter what the user is doing or what activity is controlling the screen. And, if the screen is locked, the alert should be displayed as soon as the user unlocks the phone.

Here is how you do it:

  • First create a new class (activity). We will call it displayAlert:
public class displayAlert extends Activity {

    private boolean doDisplayAlert;

    private static final boolean D = true;
    private static final String TAG = "tagYouUseForYourApp";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(D) Log.i(TAG, "+++ ALERT starting alert class. +++");
        if(D) Log.i(TAG, "+++ ALERT getting Intent. +++");

        //Get intent and boolean "proceed" field within.
        Bundle bundle = getIntent().getExtras();
        displayAlert = bundle.getBoolean("proceed");

        if(D) Log.i(TAG, "+++ ALERT got Intent. +++");

        if(displayAlert == true){

             if(D) Log.i(TAG, "+++ ALERT do display alert. +++");

             // custom dialog
             AlertDialog.Builder dialog = new AlertDialog.Builder(this);
             dialog.setTitle("Write here whatever you want to display in the alert message as title.");
             dialog.setMessage("Text content of the alert message.");

             dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int whichButton) {
                      //Do something when the user clicks OK to close the alert (optional)

                      finish();
                 }
             });

             dialog.setNegativeButton("Ignore", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int which) {
                     //Do something if the user chooses to ignore the alert (optional)
                     finish();
                 }
             });

             dialog.show();

        }else{

             if(D) Log.i(TAG, "+++ ALERT do not display alert. +++");

             //Do something in the case the service does not want you to display an alert.
             //Actually, you can use this code to display two types of alert depending on the
             //value of the boolean that the service passes to this class ("proceed")
        }

    }

}
  • Now, in the code of your service you will have something running. If you want to display the alert, you use the following code:
if(D) Log.i(TAG, "+++ SERVICE starting displayAlert activity to show an alert. +++");
Intent showAlert = new Intent(applicationContext,displayAlert.class);
showAlert.putExtra("proceed", true);
showAlert.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
applicationContext.startActivity(showAlert);
if(D) Log.i(TAG, "+++ SERVICE displayAlert activity went well. +++");
  • Note that this code needs the context of the parent application (applicationContext). What I do is to pass the context when I create and start the service.
  • Remember to define the class in the manifest file (add this code within the <application>. There should be first your main activity, then the service and then you can add the displayAlert activity)
         <activity
android:name="com.test.nameofyourapp.displayAlert"
android:label="@string/AlertActivityName" >
</activity>
  • That’s it!

I hope this is useful.

Pennywise is on shuffle on Spotify at the moment. Have a good one!



Advertisements