2013-04-23 87 views
0

我需要在用戶登錄之前計算用戶的當前經緯度。我已經在我的移動設備中測試了我的代碼,但它似乎不起作用。這裏是我的代碼:當活動恢復時GPS不工作

 LocationManager mlocManager=null; 
    LocationListener mlocListener=null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

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

    mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);   
    mlocListener = new MyLocationListener(); 
    } 

    @Override protected void onResume() {  
    super.onResume();  
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, mlocListener); 
    } 

    @Override protected void onPause() {  
    super.onPause();  
    mlocManager.removeUpdates(mlocListener); //<8>  
    } 

    /* Class My Location Listener */ 
    public class MyLocationListener implements LocationListener 
    { 

     @Override 
     public void onLocationChanged(Location loc) 
     {   
     Toast.makeText(getApplicationContext(),"In onchange", Toast.LENGTH_SHORT).show(); 
     if(loc!=null){                 

      latitude=loc.getLatitude(); 
      longitude=loc.getLongitude(); 

      if(loc.getLatitude()!=0.0 || loc.getLongitude()!=0.0){  

       Toast.makeText(getApplicationContext(),"Location not null", Toast.LENGTH_SHORT).show(); 
       SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);  
       SharedPreferences.Editor e = prefsSaveLatLong.edit(); 
       e.remove("LAT"); 
       e.remove("LONG"); 
       e.putString("LAT",Double.toString(loc.getLatitude())); 
       e.putString("LONG",Double.toString(loc.getLongitude())); 
       e.commit(); 

       String Text = "My current location is: " + "Latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude(); 
       Toast.makeText(getApplicationContext(),Text+" "+latitude+" "+longitude, Toast.LENGTH_SHORT).show(); 

      }else{ 

       SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE); 
       if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){ 

     SharedPreferences.Editor e1 = prefsSaveLatLong.edit(); 
        e1.remove("LAT"); 
        e1.remove("LONG"); 
        e1.commit(); 
       } 
      } 
      // set latitude longitude to label 
      setLatLongLabel(); 

     }else{ 
      latLongLabel.setTextColor(Color.parseColor("#FF0000")); 
      latLongLabel.setText("Latitude-Longitude not available"); 

      SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE); 
      if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){ 
       SharedPreferences.Editor e1 = prefsSaveLatLong.edit(); 
       e1.remove("LAT"); 
       e1.remove("LONG"); 
       e1.commit(); 
      } 
     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) 
    { 
     gpsEnabled=false;    
     if(!gpsEnabled){    
      Toast.makeText(getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT).show(); 
      showSettingsAlert(); 
     }  
    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
     gpsEnabled=true;   
     Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
     if(status==0){ 
      Toast.makeText(getApplicationContext(),"OUT_OF_SERVICE",Toast.LENGTH_SHORT).show(); 
      SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE); 
      if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){ 
       SharedPreferences.Editor e1 = prefsSaveLatLong.edit(); 
       e1.remove("LAT"); 
       e1.remove("LONG"); 
       e1.commit(); 
      } 
     } 
     else if(status==1){ 
      Toast.makeText(getApplicationContext(),"TEMPORARILY_UNAVAILABLE",Toast.LENGTH_SHORT).show();    
     }else if(status==2){ 
      Toast.makeText(getApplicationContext(),"AVAILABLE",Toast.LENGTH_SHORT).show(); 
     }  
    } 

    }/* End of Class MyLocationListener */enter code here 

` 
+0

'...這似乎並沒有work'沒有幫助。它怎麼不工作?有沒有例外?代碼是否運行但不返回值?你能詳細說明嗎? – psubsee2003 2013-04-23 04:59:33

+0

onResume()上的問題到底是什麼?任何錯誤或者您不會將位置設置爲空? – 2013-04-23 05:04:41

回答

1

嘗試一些這樣的事

public class ShowLocationActivity extends Activity implements LocationListener { 
     private TextView latituteField; 
     private TextView longitudeField; 
     private LocationManager locationManager; 
     private String provider; 


    /** Called when the activity is first created. */ 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     latituteField = (TextView) findViewById(R.id.TextView02); 
     longitudeField = (TextView) findViewById(R.id.TextView04); 

     // Get the location manager 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     // Define the criteria how to select the locatioin provider -> use 
     // default 
     Criteria criteria = new Criteria(); 
     provider = locationManager.getBestProvider(criteria, false); 
     Location location = locationManager.getLastKnownLocation(provider); 

     // Initialize the location fields 
     if (location != null) { 
      System.out.println("Provider " + provider + " has been selected."); 
      onLocationChanged(location); 
     } else { 
      latituteField.setText("Location not available"); 
      longitudeField.setText("Location not available"); 
     } 
     } 

     /* Request updates at startup */ 
     @Override 
     protected void onResume() { 
     super.onResume(); 
     locationManager.requestLocationUpdates(provider, 400, 1, this); 
     } 

     /* Remove the locationlistener updates when Activity is paused */ 
     @Override 
     protected void onPause() { 
     super.onPause(); 
     locationManager.removeUpdates(this); 
     } 

     @Override 
     public void onLocationChanged(Location location) { 
     int lat = (int) (location.getLatitude()); 
     int lng = (int) (location.getLongitude()); 
     latituteField.setText(String.valueOf(lat)); 
     longitudeField.setText(String.valueOf(lng)); 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
     Toast.makeText(this, "Enabled new provider " + provider, 
      Toast.LENGTH_SHORT).show(); 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 
     Toast.makeText(this, "Disabled provider " + provider, 
      Toast.LENGTH_SHORT).show(); 
     } 
    } 

this教程

+0

非常感謝!我的問題大部分都解決了。現在我需要清除lat lng incase位置不可用。我怎麼做 ?我宣佈經緯度爲主要類的局部變量 – 2013-04-23 06:44:07

+0

使用http://developer.android.com/reference/android/location/GpsStatus.Listener.html#onGpsStatusChanged%28int%29 – 2013-04-23 06:49:36