2012-02-24 69 views
0

當我在我的android手機上測試此代碼時,當前位置不顯示。我應該配置我的手機還是將新代碼增加到代碼中以允許它正確運行在我的手機上。一個提示將不勝感激。代碼如下在Android手機上測試GPS當前位置

public class GetLocation extends Activity { 

    TextView tv; 
    double latitude,longitude,accuracy; 
    private LocationManager lm; 
    private LocationListener lo; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     tv = (TextView)this.findViewById(R.id.txtLocation); 
     lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     lo = new mylocationlistener(); 
     tv.setText("waiting for location"); 
     if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 

      createGpsDisabledAlert(); 

      } 

    } 


    //dialog to check if gps enabled or not 
    private void createGpsDisabledAlert() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Your GPS is disabled! Would you like to enable it?") 
     .setCancelable(false) 
     .setPositiveButton("Enable GPS", 
     new DialogInterface.OnClickListener(){ 
      public void onClick(DialogInterface dialog, int id){ 
       showGpsOptions(); 
      } 
     }); 
     builder.setNegativeButton("Do nothing", 
       new DialogInterface.OnClickListener(){ 
       public void onClick(DialogInterface dialog, int id){ 
        dialog.cancel(); 
        } 
        }); //close negative builder 

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


    private void showGpsOptions(){ 
     Intent gpsOptionsIntent = new Intent( 
     android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     startActivity(gpsOptionsIntent); 

    } 

    private class mylocationlistener implements LocationListener { 

     public void onLocationChanged(Location location) { 


      if (location !=null){ 


      longitude = location.getLongitude(); 
      latitude = location.getLatitude(); 
      accuracy = location.getAccuracy(); 
        /* 
        Log.d("LOCATION CHANGED", latitude + ""); 
        Log.d("LOCATION CHANGED", longitude + ""); 

        */ String str = "\n CurrentLocation: "+ 
         "\n Latitude: "+ latitude + 
         "\n Longitude: " + longitude + 
         "\n Accuracy: " + accuracy; 

         Toast.makeText(GetLocation.this,str,Toast.LENGTH_LONG).show(); 
         tv.append(str);    

      } 

     } 

     public void onProviderDisabled(String provider) { 
      Toast.makeText(GetLocation.this,"Error onProviderDisabled",Toast.LENGTH_LONG).show(); 

     } 

     public void onProviderEnabled(String provider) { 
      Toast.makeText(GetLocation.this,"onProviderEnabled",Toast.LENGTH_LONG).show(); 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
      Toast.makeText(GetLocation.this,"onStatusChanged",Toast.LENGTH_LONG).show(); 
     } 

} 

    @Override 
    public void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 

    } 


    public void onResume(){ 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lo); 
     super.onResume(); 


    } 



} 
+0

你有任何異常? – Praveenkumar 2012-02-24 07:09:14

+0

沒有一切工作正常,但在烤麪包時不顯示座標 – 2012-02-24 07:12:25

+0

因此,狀態欄中出現「衛星」圖標,您需要等待很長時間才能確定位置,對不對? – 2012-02-24 07:26:36

回答

0

在你的onCreate,你需要調用lm.requestLocationUpdates()來獲取你的位置監聽器的工作。見Requesting Location Updates

+0

另外,我只需要問,你的TextView是單行嗎?如果出現這種情況,它可能會更新,但您無法看到它,因爲文本超出了TextView的範圍。 – Maz 2012-02-24 07:59:52

+0

如果將lm.requestLocationUpdates()放置在onCreate中,則程序包崩潰。如果我把onResume()放在onCreate()方法中,這是否等同?我試過它的作品,但從來不知道。 – 2012-02-24 08:10:52

+0

你可以驗證你的onLocationChanged()函數是否被調用過? – Maz 2012-02-24 08:25:36