gpt4 book ai didi

android - 有时在我重新启动手机之前我的位置是空的,为什么?

转载 作者:行者123 更新时间:2023-11-29 21:13:18 26 4
gpt4 key购买 nike

所以我得到的经度和纬度为:

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

// Define a listener that responds to location updates
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
longitude=location.getLongitude();
latitude=location.getLatitude();

}


public void onStatusChanged(String provider, int status, Bundle extras) {}

public void onProviderEnabled(String provider) {}

public void onProviderDisabled(String provider) {}
};

//Or use LocationManager.GPS_PROVIDER
String locationProvider = LocationManager.NETWORK_PROVIDER;

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

if(lastKnownLocation!=null){
longitude=lastKnownLocation.getLongitude();
latitude=lastKnownLocation.getLatitude();

}

然后我根据这些信息获取我的位置:

Geocoder myLocation = new Geocoder(Time.this, Locale.getDefault());
List<Address> myList=null;
try {
myList = myLocation.getFromLocation(latitude,longitude, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if(myList != null && myList.size()>0) {
address= (Address) myList.get(0);


if(address.getAddressLine(0)!=null){
addressStr += address.getAddressLine(0);

}

if(address.getAddressLine(1)!=null){
addressStr += ", "+address.getAddressLine(1);

}
if(address.getAddressLine(2)!=null){
addressStr += ", " +address.getAddressLine(2);

}

}

但有时在我重新启动手机之前位置会保持为空,为什么会这样?有办法解决吗?

最佳答案

尝试如下设置您的Location Listener:

public class BasicMapActivity_new2 extends Activity implements
LocationListener {

private LocationManager locationManager;
private String provider;
Double Latitude, longitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);

LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabledGPS = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean enabledWiFi = service
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!enabledGPS) {
Toast.makeText(BasicMapActivity_new2.this, "GPS signal not found",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else if (!enabledWiFi) {
Toast.makeText(BasicMapActivity_new2.this,
"Network signal not found", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}

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);

if (location != null) {

onLocationChanged(location);
} else {

// do something
}
}

@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}

@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}

Location old_one;

@Override
public void onLocationChanged(Location location) {

double lat = location.getLatitude();
double lng = location.getLongitude();
// Toast.makeText(BasicMapActivity_new.this, "Location " + lat+","+lng,
// Toast.LENGTH_LONG).show();
LatLng coordinate = new LatLng(lat, lng);
Latitude = lat;
longitude = lng;

Toast.makeText(BasicMapActivity_new2.this,
"Location " + coordinate.latitude + "," + coordinate.longitude,
Toast.LENGTH_LONG).show();

}


@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

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

}

}

并且不要忘记将 permission 添加到 manifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

更新:这是因为将requestLocationUpdates()添加到onResume()removeUpdates(this); 进入 onPause()。这样,您的应用程序将在不活动时停止更新位置。将以下内容添加到您的 Activity:

 @Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}

@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}

关于android - 有时在我重新启动手机之前我的位置是空的,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22125595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com