2014-07-02 116 views
-1

我正在開發一個簡單的「簽入」應用程序,我正面臨一些麻煩。Android位置應用程序凍結

當我點擊一個的ImageButton拿到的位置,我的logcat開始響應:

GC_EXPLICIT freed 365K, 18% free 14183K/17256K, paused 2ms+4ms, total 24ms 
to [...] 
GC_EXPLICIT freed 137K, 1% free 17871K/18044K, paused 2ms+16ms, total 63ms 

和應用程序凍結。 我減少了滯後改變RelativeLayout LinearLayout,但問題依然存在。 * PS:運行於摩托G(摩托羅拉XT1033) 代碼:

// MyLocationListener

public class MyLocationListener implements LocationListener{ 

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters 
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 5000; // in Milliseconds 
private LocationManager locationManager; 

public MyLocationListener(LocationManager locationManager) { 
    this.locationManager = locationManager; 
} 

public void onLocationChanged(Location location) { 
    locationManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER, 
      MINIMUM_TIME_BETWEEN_UPDATES, 
      MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
      new MyLocationListener(locationManager) 
    ); 

} 

public void onProviderDisabled(String s) { 


} 

public void onProviderEnabled(String s) { 


} 

public void onStatusChanged(String s, int i, Bundle b) { 


} 

} 

// CheckInActivity

public class CheckInActivity extends ActionBarActivity{ 
private SharedPreferences sharedPref; 
private LocationManager locationManager; 

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_checkin); 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.hide(); 
    sharedPref = this.getSharedPreferences("user", Context.MODE_PRIVATE); 
    String truck = sharedPref.getString("name", ""); 

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

    if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new MyLocationListener(locationManager)); 
    } else if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) { 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new MyLocationListener(locationManager)); 
    } else { 
     locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, new MyLocationListener(locationManager)); 
    } 

    EditText folk = (EditText)findViewById(R.id.folkName); 
    folk.setText(truck); 
} 

public void setLocation(View view) { 
    final ProgressDialog progressDialog = new ProgressDialog(CheckInActivity.this); 
    final EditText addressInput = (EditText)findViewById(R.id.addressInput); 
    progressDialog.setMessage("Obtendo endereço atual…"); 
    progressDialog.show(); 

    new AsyncTask<Void, Void, Location>(){ 

     @SuppressLint("NewApi") 
     @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
     @Override 
     protected Location doInBackground(Void... voids) { 
      android.location.Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

      if (location == null) 
       location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

      if (location == null) 
       location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 


      System.out.println(location); 

      if(location != null){ 
       double x = location.getLatitude(); 
       double y = location.getLongitude(); 


       final Location finalLocation = location; 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         double x = finalLocation.getLatitude(); 
         double y = finalLocation.getLongitude(); 
         String x_val = String.valueOf(x); 
         TextView lat = (TextView)findViewById(R.id.lat); 
         lat.setText(x_val); 

         String y_val = String.valueOf(y); 
         TextView lon = (TextView)findViewById(R.id.lon); 
         lon.setText(y_val); 
        } 
       }); 
       System.out.println(x); 
       System.out.println(y); 

       try{ 
        Geocoder geocoder = new Geocoder(CheckInActivity.this, Locale.getDefault()); 
        List<Address> addresses = geocoder.getFromLocation(x, y, 1); 
        final StringBuilder str = new StringBuilder(); 

        if (Geocoder.isPresent()) { 
         Address returnAddress = addresses.get(0); 

         String cidade = returnAddress.getAdminArea(); 
         String endereco = returnAddress.getAddressLine(0); 
         str.append(endereco).append(" - ").append(cidade); 


         progressDialog.dismiss(); 
         runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           addressInput.setText(str); 
          } 
         }); 

        }else{ 
         Toast.makeText(CheckInActivity.this, 
           "geocoder not present", Toast.LENGTH_SHORT).show(); 
        } 


       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

      } 
      return location; 
     } 
    }.execute(); 
} 
} 

回答

1

我認爲這個問題是在這裏:

public void onLocationChanged(Location location) { 
    locationManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER, 
      MINIMUM_TIME_BETWEEN_UPDATES, 
      MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
      new MyLocationListener(locationManager) 
    ); 
} 

每當您獲取位置更新時,您都會註冊一個新的位置監聽器,所以下一次您將獲得兩個位置更新並註冊兩個位置監聽器,然後是四個,然後是8,然後系統停止。

+0

以你的答案爲基礎我只是把一些價值: 私人靜態最後長MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; 現在有50米,聽衆不會死於循環。 –