-1
@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");
         }

     }
4

1 回答 1

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

它可能会起作用。

于 2012-07-17T10:27:22.993 回答