我有一个问题,
当我在模拟器中测试它并在DDMS屏幕中编辑位置时,locationmanager工作得非常好,但是当我在三星Galaxy SII 上测试它时,它的工作正常...
请帮帮我。
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class LbsGeocodingActivity extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
protected LocationManager locationManager;
protected Button retrieveLocationButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());
retrieveLocationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
}
});
}
public String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format("Current Location \n Longitude: %1$s \n Latitude: %2$s \n %3$s ", location.getLongitude(), location.getLatitude(), getMyPhoneNumber());
Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s \n %3$s ",
location.getLongitude(), location.getLatitude(), getMyPhoneNumber()
);
Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(LbsGeocodingActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(LbsGeocodingActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
在我拥有的权限中:
ACCES_COURSE_LOCATION
ACCES_FINE_LOCATION
答案 0 :(得分:5)
也许您正在室内进行测试,无法获取任何GPS信息。
尝试更改LocationManager.GPS_PROVIDER-->LocationManager.NETWORK_PROVIDER.
答案 1 :(得分:2)
使用String.format()
时可能会出现问题。有时,这可能会导致奇怪的,特定于设备的问题。尝试,
String lon = "" + location.getLongitude();
String lat = "" + location.getLatitude();
String num = getMyPhoneNumber();
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s \n %3$s",
lon,
lat,
num
);
您是否尝试过使用此功能?
LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(crit, true);
Location loc = lm.getLastKnownLocation(provider);
答案 2 :(得分:0)
我在我的HTC令人难以置信的S运行4.0.4上使用locationmanager时遇到了类似的问题。 它会在一段时间后停止在真实设备上触发位置更改。 在其他一些设备上,它可以工作。 很间歇。
我的解决方案是惹恼LocationManager并使用LocationClient pardiagm。 http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html
自从谷歌播放服务推出以来,位置管理器一直在我的设备上断断续续。我无法解释为什么会发生这种情况。但是,LocationClient每次都能正常工作,它会从位置管理中删除愚蠢的“提供者”概念。