Java 位置在第一次启动时返回null
对于我的应用程序,我需要获取用户的位置,以便从服务器获取相应的数据。问题是我使用的代码在第一次打开时没有正确返回位置。重新启动应用程序后,它工作正常。我搜索了其他问题,发现如果您使用Java 位置在第一次启动时返回null,java,android,Java,Android,对于我的应用程序,我需要获取用户的位置,以便从服务器获取相应的数据。问题是我使用的代码在第一次打开时没有正确返回位置。重新启动应用程序后,它工作正常。我搜索了其他问题,发现如果您使用getLastKnownLocation,并且自上次重新启动后没有位置,它将返回null。但为什么在应用程序重启时它还能工作呢?它是否在第一次打开时提取,如何让它等到第一次打开时正确提取位置 我的主要活动的代码: if (ContextCompat.checkSelfPermission(getActivity(),
getLastKnownLocation
,并且自上次重新启动后没有位置,它将返回null。但为什么在应用程序重启时它还能工作呢?它是否在第一次打开时提取,如何让它等到第一次打开时正确提取位置
我的主要活动的代码:
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);
} else {
progressBar.setVisibility(View.VISIBLE);
GPSClass gt = new GPSClass(getActivity().getApplicationContext());
Location location = gt.getLocation();
if (location == null) {
//Toast
} else {
lat = location.getLatitude();
lon = location.getLongitude();
}
new GetContacts().execute();
}
和GPSClass:
public class GPSClass implements LocationListener {
Context context;
public GPSClass(Context context) {
super();
this.context = context;
}
public Location getLocation(){
if (ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
Log.e("fist","error");
return null;
}
try {
LocationManager lm = (LocationManager) context.getSystemService(LOCATION_SERVICE);
boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled){
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,10,this);
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return loc;
}else{
Log.e("sec","error");
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
我共享了一个教程链接,请跟随该链接
调用下面的方法。它将返回值;当找到lat和long时。在对象为null或不返回任何值之前,您的应用程序必须处于空闲状态,或者显示一些进度值,或者告诉用户等待
InitGeoLocationUpdate.locationInit(SplashScreen.this,
object -> {
double latitude = object.latitude;
double longitude = object.longitude;
Lg.INSTANCE.d(TAG, "Current Location Latitude: " + latitude + " Longitude: " +
longitude);
});
尝试此操作将不会为您提供空的当前位置
class GetLastLocation extends TimerTask {
LocationManager mlocManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListenerTmp = new CustomLocationListener();
private final Handler mLocHandler;
public GetLastLocation(Handler mLocHandler) {
this.mLocHandler = mLocHandler;
}
@Override
public void run() {
timer.cancel();
mlocManager.removeUpdates(mlocListenerTmp);
Location location = mlocManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
{
if (mlocListenerTmp != null) {
mlocManager.removeUpdates(mlocListenerTmp);
}
currentLocation = location;
}
if (location != null) {
String message = String.format(
"Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Log.d("loc", " :" + message);
Bundle b = new Bundle();
{
b.putBoolean("locationRetrieved", true);
{
Message msg = Message.obtain();
{
msg.setData(b);
mLocHandler.sendMessage(msg);
}
}
}
} else {
Log.d(
"loc",
":No GPS or network signal please fill the location manually!");
location = mlocManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
currentLocation = location;
Bundle b = new Bundle();
{
b.putBoolean("locationRetrieved", true);
{
Message msg = Message.obtain();
{
msg.setData(b);
mLocHandler.sendMessage(msg);
}
}
}
} else {
Bundle b = new Bundle();
{
b.putBoolean("locationRetrieved", false);
{
Message msg = Message.obtain();
{
msg.setData(b);
mLocHandler.sendMessage(msg);
}
}
}
}
}
}
}
在您的main活动中这样称呼它
timer.schedule(new GetLastLocation(mLocHandler), 3000);
customLocationclass如下所示:
public class CustomLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
currentLocation = loc;
String Text = "My current location is: " + "Latitud = "
+ loc.getLatitude() + "Longitud = " + loc.getLongitude();
Log.d("loc", "onLocationChanged" + Text);
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
查看此链接当GPS类中的位置发生更改时,我如何通知我的MainActivity?当在另一个类(在GPSClass中)中获取位置时,我如何在类(此处为MainActivity)内使用此方法?在任何地方使用此方法。您可以使用onResume方法。最好是这样。在调用该方法之前,请确保您实现了运行时权限。好的,谢谢。我实际上不熟悉Kotlin,所以你能告诉我我必须添加什么到我的GPS类中,以便我可以在我的活动中调用函数吗?这是一个util类。复制粘贴就行了。我在这里添加的函数,从你的onresume函数调用它。好的,谢谢你,我会试试的!添加普通类和util类有区别吗?谢谢,这很有帮助!您是否考虑将
类GetLastLocation
作为嵌套类或spereate类?我也这么认为,但经过几次尝试后,仍然存在一些编译错误。我为CustomLocationListener和GetLastLocation类创建了一个不同的类。我做了所有丢失的声明,现在在MainActivity调用timer.schedule中(newgetlastLocation(mLocHandler),3000)代码>表示符号无法解析。你能告诉我怎么办吗?如何在MainActivity中初始化它?就在错误行之前添加Timer Timer=new Timer()代码>我的道歉,我的意思是我无法解决问题,而不是计时器!您是否在项目的AndroidManifest.xml
中添加了GetLastLocation
活动?