Pregunta

I want to display the dialog with two buttons "ENABLE GPS" and "CANCEL" as soon as the application starts before the Launcher activity if the GPS is off otherwise if GPS is on, the launcher activity gets opened.And when user clicks on "Enable GPS" it will it to the GPS settings and when User clicks Cancel, I want to display another Dialog Titled "how to user App" with Button "OK" and when user clicks "Ok" i want the launcher activity to gets opened. For this i am using the following code:

    if (!isGPSEnabled) {
        // no GPS provider is enabled
        // Creating the Dialog box
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                .create();
        alertDialog.setTitle("Settings");
        alertDialog.setMessage("Enable GPS for accessing the Application");
        alertDialog.setButton("GPS Settings",
                new DialogInterface.OnClickListener() {
                    // creating Button i.e GPS Setting in the Dialog Box
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                        startActivity(new Intent(
                                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

                    }
                });
        alertDialog.show();
        // creating Button i.e Cancel in the Dialog Box
        alertDialog.setButton2("Cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        // Creating another Dialog when "Cancel" is pressed
                        AlertDialog whencancelpressed = new AlertDialog.Builder(
                                MainActivity.this).create();
                        whencancelpressed.setTitle("How to use the app");
                        whencancelpressed
                                .setMessage("Enter the contacts to whom you want to send the message.Double tap power button in emergency");
                        whencancelpressed.setButton("Ok",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        // TODO Auto-generated method stub
                                        Intent pressedokay = new Intent(
                                                getApplicationContext(),
                                                MainActivity.class);
                                        startActivity(pressedokay);
                                    }
                                });
                        whencancelpressed.show();
                    }
                });

    } else {
        // GPS provider is enabled
    }

Problems that i am facing is, i am not able to make "cancel" button display and when after going through several tutorials , i tried to change the setButton to setPositiveButton , eclipse is showing me error.

¿Fue útil?

Solución

This code is working 100% just fit it as you like. All this where c is passed as a context. Declare this in your activity snd replace activity name with your activities name:

Context c = ActivityName.this;

  AlertDialog.Builder builder = new AlertDialog.Builder(c);

            builder.setTitle(R.string.confirm);
            builder.setMessage(R.string.deleteConfirm);

            builder.setPositiveButton(R.string.deleteBtnTxt,

            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                      startActivity(new Intent(
                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

                    dialog.dismiss();
                }

            });

            builder.setNegativeButton(R.string.no,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            dialog.dismiss();
                        }
                    });

            AlertDialog alert = builder.create();
            alert.show();

Otros consejos

Try to set your cancel button before showing your Dialog, like this:

    if (!isGPSEnabled) {
            // no GPS provider is enabled
            // Creating the Dialog box
            AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                    .create();
            alertDialog.setTitle("Settings");
            alertDialog.setMessage("Enable GPS for accessing the Application");
            alertDialog.setPositiveButton("GPS Settings",
                    new DialogInterface.OnClickListener() {
                        // creating Button i.e GPS Setting in the Dialog Box
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                            startActivity(new Intent(
                                    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

                        }
                    });
// creating Button i.e Cancel in the Dialog Box
    alertDialog.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            // Creating another Dialog when "Cancel" is pressed
                            AlertDialog whencancelpressed = new AlertDialog.Builder(
                                    MainActivity.this).create();
                            whencancelpressed.setTitle("How to use the app");
                            whencancelpressed
                                    .setMessage("Enter the contacts to whom you want to send the message.Double tap power button in emergency");
                            whencancelpressed.setButton("Ok",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            // TODO Auto-generated method stub
                                            Intent pressedokay = new Intent(
                                                    getApplicationContext(),
                                                    MainActivity.class);
                                            startActivity(pressedokay);
                                        }
                                    });
                            whencancelpressed.show();
                        }
                    });
            alertDialog.show();



        } else {
            // GPS provider is enabled
        }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top