2012-07-17 55 views
-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"); 
     } 

    } 
+0

您是否收到任何錯誤/異常?您是否添加了gps權限?代碼聖牆,蝙蝠俠! – 2012-07-17 05:33:49

+1

聖牆代碼,蝙蝠俠!你的問題到底是什麼? – gobernador 2012-07-17 05:34:01

+0

而不是AlertDialog alertDialog = alertDialogBu​​ilder.create(); 只寫alertDialogBu​​ilder.create(); – AkashG 2012-07-17 05:35:34

回答

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

它可能工作。

相關問題