Android应用程序未显示用于启用gps的alertBox

时间:2012-07-17 05:31:25

标签: android location

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Log.i(TAG, "INSIDE onCreate");

    //ACCESSING LOCATION MANAGER


    String svcName=Context.LOCATION_SERVICE;
    locationmanager=(LocationManager)getSystemService(svcName);


    //SPECIFY THE CRITERIA

    criteria.setAccuracy(Criteria.ACCURACY_MEDIUM);
    criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setSpeedRequired(false);
    criteria.setCostAllowed(false);


    //get the list of providers



    provider=locationmanager.getBestProvider(criteria, true);

     if(provider==null)
     {
         Log.i(TAG, "inside enable gps");
         final boolean enablegps = locationmanager.isProviderEnabled(LocationManager.GPS_PROVIDER);
         //show dialog box and enable gps..
         if(!enablegps)
         {

             Log.i(TAG, "dialog box");

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Wmi2Activity.this);


        // set title
        alertDialogBuilder.setTitle("Is GPS Enable??");

        // set dialog message
        alertDialogBuilder
                         .setMessage("Click yes to exit!")
                         .setCancelable(false)
                          .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog,final int id) {

                                             //gotoEnablrGPS();
                     dialog.cancel();

                }
              });
            alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog,final int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
}


                }
         else{
         mylocation.setText("no location provider available");
         }

     }

1 个答案:

答案 0 :(得分:0)

更改代码:

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Wmi2Activity.this);

到:

alertDialog.show();

到此代码:

new AlertDialog.Builder().setTitle("Is GPS Enable??")       
                     .setMessage("Click yes to exit!")
                     .setCancelable(false)
                      .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(final DialogInterface dialog,final int id) {

                                         //gotoEnablrGPS();
                 dialog.cancel();

            }
          }).setNegativeButton("No",new DialogInterface.OnClickListener()     {
            public void onClick(final DialogInterface dialog,final int id) {
                // if this button is clicked, just close
                // the dialog box and do nothing
                dialog.cancel();
            }
        }).create().show();

它可能有用。