how to run locationlistener in background

徘徊边缘 提交于 2019-12-13 00:34:40

问题


I would like to run a locationlistener on my app, that send after every 10m or every 5 seconds an new "Popup". Later I will send my data to the cloud.

This is my GPSTracker class:

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class GPSTracker extends Service implements LocationListener {

    private final Context mContext;

    // flag for GPS status
    boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 5 * 1; // 1 minute

    // Declaring a Location Manager
    protected LocationManager locationManager;

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();


                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

    /**
     * Stop using GPS listener
     * Calling this function will stop using GPS in your app
     * */
    public void stopUsingGPS(){
        if(locationManager != null){
            locationManager.removeUpdates(GPSTracker.this);
        }
    }

    /**
     * Function to get latitude
     * */
    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }

        // return latitude
        return latitude;
    }

    /**
     * Function to get longitude
     * */
    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }

        // return longitude
        return longitude;
    }

    /**
     * Function to check GPS/wifi enabled
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    /**
     * Function to show settings alert dialog
     * On pressing Settings button will lauch Settings Options
     * */
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        // Setting Dialog Title
        alertDialog.setTitle("GPS is settings");

        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        // Showing Alert Message
        alertDialog.show();
    }

    @Override
    public void onLocationChanged(Location location) {


    }

    @Override
    public void onProviderDisabled(String provider) {


    }

    @Override
    public void onProviderEnabled(String provider) {

        Toast.makeText(GPSTracker.this,"Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
    }

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

    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

For my activityclass I have made this:

gps = new GPSTracker(MainActivity.this);
double latitude2 = gps.getLatitude();
double longitude2 = gps.getLongitude();

I receive the data, this works. But now I want to run the data in the background so when the location is changed I can implement a function that sends my data to the cloud.

I tried this:

private void startGPSTrackerInBackground() {
    new AsyncTask<Void,Void,String>() {

        @Override
        protected String doInBackground(Void... params) {
            String msg = "This message runs in background";


            gps = new GPSTracker(MainActivity.this);

            if(!gps.canGetLocation()){
                gps.showSettingsAlert();
            }else{
                gps = new GPSTracker(MainActivity.this);
                double latitude2 = gps.getLatitude();
                double longitude2 = gps.getLongitude();
            }
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
            mDisplay.append(msg + "\n");
        }
    }.execute(null, null, null);

}

But in this function it's not possible to get the information of my GPSTracker class, anyone an idea to make this possible?


回答1:


Create an independent class, add a GPSTracker member variable and pass it in the constructor. Wrap your task in a method you can invoke later:

public class BackgroundGPSTracker() {
    GPSTracker tracker;

    public BackgroundGPSTracker(GPSTracker tracker) {
        this.tracker = tracker;
    }

    public void run() {
        new AsyncTask<Void,Void,String>() {
            @Override
            protected String doInBackground(Void... params) {
                // Do some background stuff.
            }

            @Override
            protected void onPostExecute(String msg) {
                // Do after work stuff
            }
        }.execute(null, null, null);
    }
}

Use:

If you create GPSTracker like this:

gps = new GPSTracker(MainActivity.this);

Then you start the activity passing it to the task:

BackgroundGPSTracker bgGPSTracker= new BackgroundGPSTracker(gps);
bgGPSTracker.run();

EDIT

Use a member variable to store last known location, and timer to schedule updates:

public class CampaignsDiscoverActivity extends Activity{
    static final int QUERY_CAMPAINGS_DELAY = 30000;// milliseconds

    Location currentLocation;

    Timer timer;

    void restartTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                sendDataToServerOrWhatever();
            }
        }, QUERY_CAMPAINGS_DELAY, QUERY_CAMPAINGS_DELAY);
    }

    void stopTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    void sendDataToServerOrWhatever() {
        // Do some stuff using currentLocation
    }

Set a listener for locations changes. when location changed is raised, stop the timer, do work and restart it:

void startGPSUpdates() {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationListener = new LocationListener() {
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onLocationChanged(Location location) {
            stopTimer();

            currentLocation = location;
            sendDataToServerOrWhatever();
        }
    };

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

    // Initialize location.
    currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (currentLocation == null) {
        currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }

    restartTimer();
}

To start the system just invoke startGPSUpdates.

You should pause listeners when app goes to the background and resume when foreground:

@Override
public void onPause() {
    super.onPause();

    if (applicationData.isLoggedIn()) {
        pauseLocationUpdates();
    }
}

@Override
public void onResume() {
    super.onResume();

    if (applicationData.isLoggedIn()) {
        resumeLocationUpdates();
    }
}

void pauseLocationUpdates() {
    locationManager.removeUpdates(locationListener);
}

void resumeLocationUpdates() {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}

I think you should do the same with the timer, but the app where I'm using it is not finished and fully tested yet, so you might find bugs.

Hope that helps.



来源:https://stackoverflow.com/questions/28546461/how-to-run-locationlistener-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!