从类访问的LocationManager / LocationListener的 [英] Access LocationManager/ LocationListener from class

查看:112
本文介绍了从类访问的LocationManager / LocationListener的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点失去了在这里:在我的主要活动,我注册的LocationManager并将其连接到一个LocationListener的使用myLocation.getLatitude()和等。

现在我需要使用位置 - 方法从另一个类。

我不能使用这些对象从另一个类,因为我不能intantiate的主要活动。
我不能用干将各地通过L.Manager或L.Listener,因为这些都是非静态的一次。

所以,总的来说,我怎么访问我的主要活动创建的对象?
如何组织这更好的任何提示吗?是主要的活动课中的LocationListener的类愚蠢的事一般做什么?

 公共类URNavActivity扩展活动{
    公众的LocationManager mlocManager;
    公共LocationListener的mlocListener;
...
}公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    mResourceProxy =新DefaultResourceProxyImpl(getApplicationContext());    actVar =这一点;    initGraph();
    的setMap();
    gpsEnable();
    initMyLocation();
    getItems();
    initOverlay();
}公共无效gpsEnable()
{
    mlocManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);    mlocListener =新MyLocationListener();    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mlocListener);
}公共类MyLocationListener实现LocationListener的{@覆盖公共无效onLocationChanged(位置LOC){    loc.getLatitude();
    loc.getLongitude();
    myMap.getController()setCenter(新的GeoPoint(LATI,茇));
}


解决方案

首先你的LocationListener的不应该是一个活动的一部分。活动有明确的生命周期,并能应运而生,并予以销毁,由Android框架的需要的基础上。因此,你的活动的实例变量将需要在活动的onResume()方法来重新初始化,使它们完全不适合长期储存。

所以。通过创建一个棘手的服务来管理的起点和位置更新的停止启动。作为粘意味着服务实例徘徊调用之间,因此可以可靠地使用实例变量,并且知道,直到服务被终止,他们将保留其值。该服务还应该实现LocationListener的界面,现在它可以存储通知给它的位置被调用时onLocationChanged:

 公共类LocationService扩展服务实现LocationListener的{    私人的LocationManager的LocationManager;    私人位置定位;    @覆盖
    公众诠释onStartCommand(最终意图的意图,最终诠释旗帜,最终诠释startId){        Logging.i(clazz所onHandleIntent,调用);        如果(intent.getAction()。等于(startListening)){
            的LocationManager =(的LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,这一点);
        }
        其他{
            如果(intent.getAction()。等于(的stopListening)){
                locationManager.removeUpdates(本);
                的LocationManager = NULL;
            }
        }        返回START_STICKY;    }    @覆盖
    公众的IBinder onBind(最终意向意图){
        返回null;
    }    公共无效onLocationChanged(决赛地点){
        this.location =位置;
        // TODO这是你应该这样做context.sendBroadcast()
    }    公共无效onProviderDisabled(最后弦乐提供商){
    }    公共无效onProviderEnabled(最后弦乐提供商){
    }    公共无效onStatusChanged(最后弦乐为arg0,最终诠释ARG1,最终捆绑ARG2){
    }}

现在你就可以开始和你需要他们停止位置更新的服务。这也可以让你继续接收,甚至当你的应用程序是不是在前台进程位置更改,如果这是你想要的。

您现在对如何使可用的位置信息两种选择:使用context.sendBroadcast()到新的位置传播到(例如)活动,或使用绑定的服务态度,让其他类调用外露API并获得位置。见<一href=\"http://developer.android.com/guide/topics/fundamentals/bound-services.html\">http://developer.android.com/guide/topics/fundamentals/bound-services.html有关创建一个绑定服务的更多细节。

请注意,还有其他许多方面监听,我这里不包括位置更新,为清楚起见。

I'm kinda lost here: In my main activity, I register a LocationManager and connect it to a LocationListener to use myLocation.getLatitude() and such.

Now I need to use the Location- methods from another class.

I can't use those object from another class because I cant intantiate the main activity. I can't use getters to pass the L.Manager or L.Listener around, because those are non- static again.

So, in general, how do i access objects that I created in the main activity? Any hints on how to organize this better? Is the LocationListener class within the main activity class a stupid thing to do in general?

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

解决方案

First and foremost your LocationListener should not be part of an activity. Activities have a clearly defined lifecycle and can come into being, and be destroyed, by the Android framework on an as-needed basis. Therefore the instance variables of your Activity will need to be re-initialised in your activity's onResume() method, making them completely unsuitable for long-term storage.

So. Start by creating a sticky service to manage the starting and stopping of location updates. Being sticky means that the service instance hangs around between invocations and therefore you can reliably use instance variables and know that they will retain their values until the service is terminated. This service should also implement the LocationListener interface, and now it can store the Location notified to it when onLocationChanged is invoked:

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) {
    }

}

Now you have a service you can start and stop the location updates as you need them. This also allows you to continue to receive and process location changes even when your application is not in the foreground, if that is what you want.

You now have two choices on how to make that Location information available: Use context.sendBroadcast() to propagate the new Location to (for example) an activity, or use the bound service approach to allow other classes to invoke the exposed API and obtain the Location. See http://developer.android.com/guide/topics/fundamentals/bound-services.html for more details on creating a bound service.

Note that there are many other aspects to listening for location updates that I have not included here, for the sake of clarity.

这篇关于从类访问的LocationManager / LocationListener的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆