Show an alertDialog from another Activity Android

If you are trying to display Alert from your running service then you have to call new custom alert dialog class that will display alert whether you are on any activity.

    public class AlertDialogActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        displayAlert();
    }

    private void displayAlert()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setInverseBackgroundForced(true);
        builder.setMessage("You Message here").setCancelable(
            false).setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    // code here
                    AlertDialogActivity.this.finish();                  
                    dialog.cancel();                 
                }
            }).setNegativeButton("Close",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    AlertDialogActivity.this.finish();
                    dialog.cancel();
                }
            });
        AlertDialog alert = builder.create();
        alert.show();
    }

}

And call this class from your service to display alert.

   Intent dialogIntent = new Intent(getApplicationContext(), AlertDialogActivity.class);
getApplication().startActivity(dialogIntent);

Leave a Comment