- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有点迷路了:在我的主要 Activity 中,我注册了一个 LocationManager 并将其连接到 LocationListener 以使用 myLocation.getLatitude() 等。
现在我需要使用另一个类中的 Location- 方法。
我无法使用其他类中的那些对象,因为我无法实例化主要 Activity 。我不能使用 getter 传递 L.Manager 或 L.Listener,因为它们又是非静态的。
那么,一般来说,我如何访问我在主要 Activity 中创建的对象?关于如何更好地组织这个的任何提示?主要 Activity 类中的 LocationListener 类通常是一件愚蠢的事情吗?
public class URNavActivity extends Activity
{
public LocationManager mlocManager;
public LocationListener mlocListener;
...
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
actVar=this;
initGraph();
setMap();
gpsEnable();
initMyLocation();
getItems();
initOverlay();
}
public void gpsEnable ()
{
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
myMap.getController().setCenter(new GeoPoint(lati, longi));
}
最佳答案
首先,您的 LocationListener 不应成为 Activity 的一部分。 Activity 有一个明确定义的生命周期,可以根据需要由 Android 框架生成和销毁。因此,您的 Activity 的实例变量将需要在您的 Activity 的 onResume() 方法中重新初始化,使它们完全不适合长期存储。
所以。首先创建一个粘性服务来管理位置更新的启动和停止。粘性意味着服务实例在调用之间挂起,因此您可以可靠地使用实例变量并且知道它们将保留它们的值直到服务终止。该服务还应该实现 LocationListener 接口(interface),现在它可以存储调用 onLocationChanged 时通知给它的位置:
public class LocationService extends Service implements LocationListener {
private LocationManager locationManager;
private Location location;
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
Logging.i(CLAZZ, "onHandleIntent", "invoked");
if (intent.getAction().equals("startListening")) {
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else {
if (intent.getAction().equals("stopListening")) {
locationManager.removeUpdates(this);
locationManager = null;
}
}
return START_STICKY;
}
@Override
public IBinder onBind(final Intent intent) {
return null;
}
public void onLocationChanged(final Location location) {
this.location = location;
// TODO this is where you'd do something like context.sendBroadcast()
}
public void onProviderDisabled(final String provider) {
}
public void onProviderEnabled(final String provider) {
}
public void onStatusChanged(final String arg0, final int arg1, final Bundle arg2) {
}
}
现在您有了一项服务,您可以根据需要启动和停止位置更新。这还允许您继续接收和处理位置更改,即使您的应用程序不在前台(如果您需要的话)。
关于如何使位置信息可用,您现在有两种选择:使用 context.sendBroadcast() 将新位置传播到(例如) Activity ,或使用绑定(bind)服务方法允许其他类调用公开的API并获取Location。参见 http://developer.android.com/guide/topics/fundamentals/bound-services.html有关创建绑定(bind)服务的更多详细信息。
请注意,为了清楚起见,我没有在此处包括许多其他方面的监听位置更新。
关于android - 从类访问 LocationManager/LocationListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7759504/
我终于找到了一个获取用户 GPS 位置的应用程序,但我找到了实现 LocationListener 的应用程序。它工作正常,但我需要在不实现它的情况下执行它,因为我必须创建一个不实现方法的类。 我搜索
出现以下错误 Error:(197, 28) error: no suitable method found for requestLocationUpdates (String,long,fl
我想问一下这两个库中的 onLocaticonChange 方法。我可以从 android.location.LocationListener 捕获位置,但是当我决定使用 google.android
这是我获取经度和纬度的源代码,但是我如何在Location Listener中实现语句,我是新手,我真的需要帮助,请提供帮助,谢谢。 public class Location extends App
我正在尝试使用 LocationManager 和 LocationListener 获取用户的当前位置。但没有获得任何值(value)。 下面是我在 Java 中使用的代码: // activi
我有一个获取用户位置的应用程序。我已经在 oncreate() 中注册了 locationManager 和 listener。我已分别请求并删除了 onResume() 和 onPause()
我正在开发一个 Android 应用程序 (minSDK=8),这个应用程序有一张 map ,上面绘制了用户的当前位置。昨天,我正在修复一些路线制定的错误,一切都很好。然后,我不知道我做了什么,但设备
我已经注册了一个位置监听器,它将使用NETWORK_PROVIDER 接收位置更新。如果 activity 在前台,监听器会收到位置更新。如果我离开应用程序,一段时间后它会停止接收位置更新。这有点奇怪
我有以下代码可以从 NETWORK_PROVIDER 和 GPS_PROVIDER 请求位置更新。 locManager.requestLocationUpdates(LocationManager.
我想每 10 秒向远程服务器发送一些数据和坐标。我想,最好的匹配是 public void onCreate( Bundle savedInstanceState ) { //snip loc
我正在尝试使用 LocationListener 获取我的 GPS 速度。 我在第一次启动应用程序时请求许可,然后授予许可并且“onLocationChanged()”立即显示 toast ,但不会再
我写了这段代码,但是 MyLocationListener 类不起作用。当我像“找我”这样的短信时,代码必须给我大概的位置,但它什么也没做。这里有什么问题吗? public class SMSRece
我有以下代码使用 GPS 获取用户位置。(代码工作正常) 但我的问题是,当我关闭程序(按后退按钮)时,它会生成错误消息 应用程序......已停止工作............请重试。 . 我认为我必须
我正在尝试调试我的应用程序,但出于某种原因,当我开始获取当前位置时,我从未收到 LocationListener 的更新...我已在 list 中添加了正确的权限。这是我用来测试是否至少调用了一个回调
我正在尝试让位置更新在后台服务中运行。该服务正在运行自己的工作线程,它已经在做很多其他事情,比如套接字通信。我希望它也能处理位置更新,但这似乎只适用于一项 Activity 。据我所知,这是由于工作线
我目前正在使用计时器来确定位置监听器是否已超时?问题是 gps 仍然打开。我不知道为什么,是否有一种方法可以在 locationlistener 超时时重写或更优雅的方法? 最佳答案 考虑向您的位置管
根据sample app找到用户位置最好在 Activity 中监听位置变化: class MyActivity extends Activity implements LocationListene
我需要在我的整个应用程序中实现一个 LocationListener。 我知道我可以将服务与 LocationListener 一起使用,但问题是我必须在位置更改时使用一些通知程序。当我能够随时获得最
我在 eclipse for android 中使用 loactionListener 时遇到问题。我已经用谷歌搜索了一段时间,但我似乎不明白为什么这不起作用。我唯一能想到的是,也许是因为我的测试设备
我有一个使用 LocationListener 的服务,它将无限期地从启动运行。我的问题是,监听位置更新的频率有多高? 5分钟是电池 killer 吗? 1 呢? 最佳答案 是的,5 分钟是电池 ki
我是一名优秀的程序员,十分优秀!