gpt4 book ai didi

java - 自定义 Android LocationListener 问题

转载 作者:搜寻专家 更新时间:2023-11-01 08:09:17 24 4
gpt4 key购买 nike

目前我有如下代码用于我在 android 上的 Activity 。现在我将其用作对象并开始扫描位置并在 3 秒后使用处理程序使用 returnBestLocation 方法取回它的位置。

但是我想问一下,MyLocationListener 对象是否有可能在位置更改时自动返回调用 Activity ,而不是在 3 秒后调用对象返回位置?

public class MyLocationListener implements LocationListener {

LocationManager locationManager;
Date currentBestLocationDate;
Intent notificationIntent;
Context mContext;
Location currentBestLocation = null, lastKnownLocation=null;

public MyLocationListener(Context mContext)
{this.mContext = mContext;

}


public void startLocationScan()
{
Log.d(Config.log_id, "Custom Location Listener started");

if (locationManager == null) {

locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
Location locationNETWORK = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (locationNETWORK != null) {
lastKnownLocation=locationNETWORK;
}
Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (locationGPS != null) {
lastKnownLocation=locationGPS;

}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, MyLocationListener.this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,MyLocationListener.this);





}


}
public void stopLocationScan()
{

if(locationManager!=null)
{
locationManager.removeUpdates(MyLocationListener.this);

Log.d(Config.log_id, "Custom Location Listener Stopped");
}
}
public Location returnBestLocation()
{
return currentBestLocation;
}

@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 onLocationChanged(Location location) {


// TODO Auto-generated method stub
if (currentBestLocation == null) {
currentBestLocation = location;
}

long timeDelta = location.getTime() - currentBestLocation.getTime();
Log.d(Config.log_id, "locationpostingservice's changed with accuracy " + location.getAccuracy() + " s different " + (float) timeDelta / 1000);

if (timeDelta >= 120000) {
currentBestLocation = location;
Log.d(Config.log_id,"posting service Location changed due to over 2min "+ location.getAccuracy() + " s different "+ (float) timeDelta / 1000);

}


if (currentBestLocation.getAccuracy() >= location.getAccuracy()) {
currentBestLocation = location;
}

}

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

}

最佳答案

您可以将要通知的 Activity 作为 Listener 传递给 MyLocationListener。创建自己的 Listener 接口(interface),让 Activity 实现它并向 MyLocationListener 添加一个方法,如 addListener()。每次您想要通知这些 Activity 时,都会遍历监听器列表并调用它们的 locationChanged 方法(或您在接口(interface)定义中调用的任何方法)。请务必为空监听器 Activity 等添加错误处理。

所以基本上您有自己的自定义监听器来监听 LocationListener。

另一种方法是使用广播接收器并广播位置变化。

关于java - 自定义 Android LocationListener 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11413901/

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