开发者

Access LocationManager/ LocationListener from class

开发者 https://www.devze.com 2023-04-13 09:15 出处:网络
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.

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.


I would offer two elegant ways to access your object from anywhere:

  1. use a Singleton design pattern
  2. use ProjectApp class. This class can be accessed from any activity simply by calling getApplication().

    ProjectApp app = (ProjectApp)getApplication();

I used a combination of the two:

    public class MyApp extends Application {

    private MyLocation mMyLocation;

    @Override
    public void onCreate() {
        super.onCreate();
        mMyLocation = new MyLocation();
        mMyLocation.getLocation(this, GlobalData.getInstance(), true);
    }
}

You can see that GlobalData is a singleton class that implements LocationResult interface, meaning that it will send the updated location to this object. When I need to get the updated location, I take it from GlobalData.

Here is MyLocation class implementation (I used some code from here and made some changes:

package com.pinhassi.android.utilslib;


import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MyLocation {
    private Timer timer1;
    private LocationManager lm;
    private LocationResult locationResult;
    private boolean gps_enabled=false;
    private boolean network_enabled=false;

    private boolean mContinuesUpdates;
    private int decimalAccuracy; 

    /**
     * Class constructor
     */
    public MyLocation(){
        decimalAccuracy = 0;
    }

    public boolean getLocation(Context context, LocationResult result, boolean continuesUpdates)
    {
        mContinuesUpdates = continuesUpdates;
        //I use LocationResult callback class to pass location value from MyLocation to user code.
        locationResult=result;
        if(lm==null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //exceptions will be thrown if provider is not permitted.
        try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

        //don't start listeners if no provider is enabled
        if(!gps_enabled && !network_enabled)
            return false;

        if(gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        if(network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        timer1=new Timer();
        timer1.schedule(new GetLastLocation(), 20000);
        return true;
    }

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(getDecimalAccurated(location));
            if (!mContinuesUpdates)
                lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);

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

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(getDecimalAccurated(location));
            if (!mContinuesUpdates)
                lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
             lm.removeUpdates(locationListenerGps);
             lm.removeUpdates(locationListenerNetwork);

             Location net_loc=null, gps_loc=null;
             if(gps_enabled)
                 gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
             if(network_enabled)
                 net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

             //if there are both values use the latest one
             if(gps_loc!=null && net_loc!=null){
                 if(gps_loc.getTime()>net_loc.getTime())
                     locationResult.gotLocation(getDecimalAccurated(gps_loc));
                 else
                     locationResult.gotLocation(getDecimalAccurated(net_loc));
                 return;
             }

             if(gps_loc!=null){
                 locationResult.gotLocation(getDecimalAccurated(gps_loc));
                 return;
             }
             if(net_loc!=null){
                 locationResult.gotLocation(getDecimalAccurated(net_loc));
                     return;
                 }
                 locationResult.gotLocation(null);
            }
        }

        /**
         * called when the GPS returns a location.
         * can be called multiple times as the location is updated
         */
        public interface LocationResult {
            public void gotLocation(Location location);
        }

        /**
         * sets location result accuracy
         * @param n number of places after the point. negative value or 0 means not set.
         */
        public void setDecimalAccuracy(int n)
        {
            this.decimalAccuracy = n;
        }

        private Location getDecimalAccurated(Location location) {
            if (decimalAccuracy > 0){
            double accuracy = Math.pow(10, this.decimalAccuracy);
            int ix;

            ix = (int)(location.getLatitude() * accuracy); 
            location.setLatitude(((double)ix)/accuracy);

            ix = (int)(location.getLongitude() * accuracy); 
            location.setLongitude(((double)ix)/accuracy);
            }

            return location;
        }

    }


I'll talk in general since I had the same issue: How to manage the LocationListener and lit this listener access the activity ..

This was my try :

The Listener :

public class MyLocationListener  implements LocationListener{
    ProgressDialog dialog;
    LocationManager locManager;
    Context context;

    public MyLocationListener  (Context context,ProgressDialog dialog){

        this.context = context;
        this.dialog = dialog;
    }  

    public void startSearch() {

        locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        // If the network provider works run it , else try GPS  provider
        // TODO : what happens if GPS and Network providers are not suuported ??


            if(!locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) )
                locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                        0, this);
            else
                locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
                        0, this);

                dialog = new ProgressDialog(context);
                dialog.setTitle("");
                dialog.setMessage(context.getString(R.string.pleaseWait));
                dialog.setButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int which) 
                    {
                        locManager.removeUpdates(MyLocationListener .this);
                        dialog.dismiss();
                        return;
                    }
                });

            dialog.show();

    } 

    // Location Listener implementation
    // read Android doc for more info
    // this methods is triggered when new location ( latitiude and longitude ) is found by the system
    private void updateWithNewLocation(Location location) {


        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();

            //THIS IS MY ACTIVITY
            MainActivity mainActivity = (MainActivity) context;
            mainActivity.init();

        } else {
            //this.setSummary( "No location found" );
        }
        // remove the listener , we don't need it anymore
        locManager.removeUpdates(this);
        dialog.hide();
    }

    public void onLocationChanged(Location location) {
        updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider) {
        updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider) {
    }

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



}

init the listener in the MainActivity like this :

ProgressDialog dialog;
.
.
.
new MyLocationListener  (this, dialog).startSearch();

I don't know if that help ? but that was my solution ...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号