我正在开发一个简单的#34; Check-In"应用程序和我面临一些麻烦。
当我点击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:在Moto 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();
}
}
答案 0 :(得分:1)
我认为问题在于:
public void onLocationChanged(Location location) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener(locationManager)
);
}
每次获得位置更新时,您都会注册一个新的位置监听器,因此下次您将获得两个位置更新并注册另外两个位置监听器,然后是四个,然后是8,那么系统将停止运行。