简体   繁体   中英

Show an alertDialog from another Activity Android

On my MainActivity I start a service of localization that runs across all of the otheractivities, when an event occurs I need to show an AlertDialog on the current activity but I can't get it working. this is the code for the alertDialog:

AlertDialog.Builder builder = new AlertDialog.Builder(mMainActivity.this);
                        builder.setMessage("test")
                           .setCancelable(false)
                           .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id) {
                                   Intent intent = new Intent(mMainActivity.this,LocationDetails.class);
                                    intent.putExtra("placeId",1);
                                    startActivity(intent);

                               }
                           })
                           .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id) {

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

Obviously this code only works if i'm on my MainActivity since I create the builder like this:

new AlertDialog.Builder(mMainActivity.this);

Is there a way to create a builder based on the current activity even if it gets created on another one?

EDIT:

I tried with AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); But it doesn't work..

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);

Try using this instead of MainActivity.this .

new AlertDialog.Builder(this)
    .setTitle("title")
    .setMessage("A message?")
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // yes pressed
        }
     })
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // no pressed
        }
     })
    .setIcon(android.R.drawable.ic_dialog_alert)
     .show();

this is a working example of custom alert dialog try this

public class ViewDialog {

public void showAlertDialog(Context context, String title, String message) {

AlertDialog alertDialog = new AlertDialog.Builder(context)

//set title
.setTitle(title)
//set message
.setMessage(message)
//set positive button
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
   //set what would happen when positive button is clicked    
        alertDialog.dismiss();
    }
})
//set negative button
.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {

//set what should happen when negative button is clicked
        alertDialog.dismiss();
    }
})
.show();
}

when you want to call this from another activity

ViewDialog  viewDialog=new ViewDialog();
viewDialog.showAlertDialog(this,"New Dialog","Message");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM