使用LocationListener在App在Android中运行后台时获取GPS数据

时间:2014-01-07 05:32:58

标签: android background gps

我的Android应用程序有问题。我需要一个应用程序,它将获得后台GPS位置,同时打开另一个application.application在后台运行,但我想在后台获得GPS位置。

创建开始和停止按钮以获取正常工作的gps位置但是当点击主页按钮时gps停止以获取位置

这是我的代码..

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_gps);

            /* get TextView to display the GPS data */
            txtLat = (TextView) findViewById(R.id.textview1);

            /* the location manager is the most vital part it allows access 
             * to location and GPS status services */
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER ,10000,10, this);
           // locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 * 1000L, 0, this);

            btnShow = (Button) findViewById(R.id.btnstart);
            btnStop = (Button) findViewById(R.id.btnstop);


            btnShow.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    Toast.makeText(getBaseContext(), "Trip Start.", Toast.LENGTH_SHORT).show();
                    txtLat.setVisibility(View.VISIBLE);
                    onResume();
                    btnShow.setEnabled(false);
                    btnStop.setEnabled(true);

                }
            });

            Button btnstop = (Button) findViewById(R.id.btnstop);
            btnstop.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                 onPause();
                 Toast.makeText(getBaseContext(), "Trip Ended.", Toast.LENGTH_SHORT).show();
                 btnShow.setEnabled(true);
                 btnStop.setEnabled(false);
                }
            });
            }
     public void onLocationChanged(Location location) {

            if (location == null) {
                  Toast.makeText(getApplicationContext(),"Searching for your location.", Toast.LENGTH_SHORT).show();

                  locationManager.requestLocationUpdates(provider, 10000, 10,locationListener);
                  onLocationChanged(location);
             }
             else
             {

            double cell_lat=location.getLatitude();
            double cell_long=location.getLongitude();
            double altitude=location.getAltitude();
            double accuracy=location.getAccuracy();                      

            String status="true";

            sb = new StringBuilder(512);

            noOfFixes++;

            /* display some of the data in the TextView */

            sb.append("Tracking: ");
            sb.append(noOfFixes);
            sb.append('\n');
            sb.append('\n');

            sb.append("Londitude: ");
            sb.append(location.getLongitude());
            sb.append('\n');

            sb.append("Latitude: ");
            sb.append(location.getLatitude());
            sb.append('\n');

            sb.append("Altitiude: ");
            sb.append(location.getAltitude());
            sb.append('\n');

            sb.append("Accuracy: ");
            sb.append(location.getAccuracy());
            sb.append("ft");
            sb.append('\n');        

            txtLat.setText(sb.toString());
    }
      public void onProviderDisabled(String provider) {
                /* this is called if/when the GPS is disabled in settings */
                Log.v(tag, "Disabled");

                /* bring up the GPS settings */
                Intent intent = new Intent(
                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);

            }

            public void onProviderEnabled(String provider) {
                Log.v(tag, "Enabled");
                Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();

            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
                /* This is called when the GPS status alters */
                switch (status) {
                case LocationProvider.OUT_OF_SERVICE:
                    Log.v(tag, "Status Changed: Out of Service");
                    Toast`enter code here`.makeText(this, "Status Changed: Out of Service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case LocationProvider.TEMPORARILY_UNAVAILABLE:
                    Log.v(tag, "Status Changed: Temporarily Unavailable");
                    Toast.makeText(this, "Status Changed: Temporarily Unavailable",
                            Toast.LENGTH_SHORT).show();
                    break;
                case LocationProvider.AVAILABLE:
                    Log.v(tag, "Status Changed: Available");
                    Toast.makeText(this, "Status Changed: Available",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
     @Override
            protected void onResume() {
                /*
                 * onResume is is always called after onStart, even if the app hasn't been
                 * paused
                 * 
                 * add location listener and request updates every 1000ms or 10m
                 */
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
                super.onResume();
            }

            @Override
            protected void onPause() {
                /* GPS, as it turns out, consumes battery like crazy */
                locationManager.removeUp`enter code here`dates(this);
                super.onPause();
            }

@Override
           public boolean onKeyDown(int keyCode, KeyEvent event) {
               if ((keyCode == KeyEvent.KEYCODE_HOME)) {
                   System.out.println("KEYCODE_HOME");                                  
                   Toast.makeText(getBaseContext(), "Home Button.", Toast.LENGTH_SHORT).show();                   
                   this.moveTaskToBack(true);
                   //showDialog("'HOME'");                   
                   return true;
               }
               if ((keyCode == KeyEvent.KEYCODE_BACK)) {
                   System.out.println("KEYCODE_BACK");                 
                  finish();
                   showDialog("'BACK'");                   
                   return true;
               }
               if ((keyCode == KeyEvent.KEYCODE_MENU)) {
                   System.out.println("KEYCODE_MENU");
                   //showDialog("'MENU'");
                   return true;
               }
               return false;
           }

               void showDialog(String the_key){
               AlertDialog.Builder builder = new AlertDialog.Builder(this);
               builder.setMessage("You have pressed the " + the_key + " button. Would you like to exit the app?")
                     .setCancelable(true)
                      .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                              dialog.cancel();
                              finish();
                          }
                      })
                      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                               dialog.cancel();
                          }
                      });
               AlertDialog alert = builder.create();    
               alert.setTitle("GPS.");
               alert.show();
           }

1 个答案:

答案 0 :(得分:1)

我发现这个代码在某个地方它对LINK有用,我根据我的要求修改了代码,试一试。

它从后台服务保存数据库中的数据,您只需要从活动启动服务,注意:只有当第一个位置的距离超过给定的最小距离时,它才会保存在数据库中。

public class BackgroundLocationService extends Service implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

private static final String TAG = "BackgroundLocationService";

IBinder mBinder = new LocalBinder();

private LocationClient mLocationClient;
private LocationRequest mLocationRequest;
// Flag that indicates if a request is underway.
private boolean mInProgress;

private Boolean servicesAvailable = false;

private double previousLatitude = 0;

private double previousLongitude = 0;

private SharedPreferences stopLocatinServicePreferance;

private float[] distance;

// public static boolean isStopSelf = false;

public class LocalBinder extends Binder {
    public BackgroundLocationService getServerInstance() {
        return BackgroundLocationService.this;
    }
}

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

    mInProgress = false;
    // Create the LocationRequest object
    mLocationRequest = LocationRequest.create();
    // Use high accuracy
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    // Set the update interval to 5 seconds
    mLocationRequest.setInterval(Constants.UPDATE_INTERVAL);
    // Set the fastest update interval to 1 second
    mLocationRequest.setFastestInterval(Constants.FASTEST_INTERVAL);

    servicesAvailable = servicesConnected();

    /*
     * Create a new location client, using the enclosing class to handle
     * callbacks.
     */
    mLocationClient = new LocationClient(this, this, this);

}

private boolean servicesConnected() {

    // Check that Google Play services is available
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);

    // If Google Play services is available
    if (ConnectionResult.SUCCESS == resultCode) {

        return true;
    } else {

        return false;
    }
}

public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    if (!servicesAvailable || mLocationClient.isConnected() || mInProgress)
        return START_STICKY;

    setUpLocationClientIfNeeded();
    if (!mLocationClient.isConnected() || !mLocationClient.isConnecting()
            && !mInProgress) {
        appendLog(DateFormat.getDateTimeInstance().format(new Date())
                + ": Started", Constants.LOG_FILE);
        mInProgress = true;
        mLocationClient.connect();
    }

    return START_STICKY;
}

/*
 * Create a new location client, using the enclosing class to handle
 * callbacks.
 */
private void setUpLocationClientIfNeeded() {
    if (mLocationClient == null)
        mLocationClient = new LocationClient(this, this, this);
}

// Define the callback method that receives location updates
@Override
public void onLocationChanged(Location location) {
    // Report to the UI that the location was updated
    String msg = Double.toString(location.getLatitude()) + ","
            + Double.toString(location.getLongitude());
    Log.d("debug", msg);
    SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
    String timeStamp = s.format(new Date());
    System.out.println("TimeStamp  ddMMyyyyhhmmss =" + timeStamp);

    if (isDistanceAccountable(location.getLatitude(),
            location.getLongitude(), previousLatitude, previousLongitude)) {
        DatabaseHelper dbm = DatabaseHelper
                .getInstance(getApplicationContext());
        ContentValues cv = new ContentValues();
        cv.put(DatabaseHelper.KEY_LATLONG, location.getLatitude() + "@"
                + location.getLongitude());
        cv.put(DatabaseHelper.KEY_TIMESTAMP, timeStamp);
        cv.put(DatabaseHelper.KEY_STATUS, "0");
        cv.put(DatabaseHelper.KEY_USER_ID, "");
        dbm.insert(DatabaseHelper.TABLE_TRACKINFO_TABLE, cv);
        // }

        // Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        appendLog(msg, Constants.LOCATION_FILE);

    }
    previousLatitude = location.getLatitude();
    previousLongitude = location.getLongitude();

    stopLocatinServicePreferance = getSharedPreferences("SERVICE_PREF", 0);
    boolean check = stopLocatinServicePreferance.getBoolean("isStop", true);
    Log.e(TAG, "isStop?" + check);
    if (check) {
        Log.e(TAG, "is self stoping ");
        if (mLocationClient != null) {
            mLocationClient.removeLocationUpdates(this);
            // Destroy the current location client
            mLocationClient = null;
        }
        this.stopSelf();
    }
}

/**
 * Returns true if distance is grater than MINIMUM_DISTANCE_ACCOUNTABLE
 * NOTE: returns true if there is no previousLatitude,previousLongitude, it
 * means this is first time it needs to save the data
 * 
 * @param currentLatitude
 * @param currentLongitude
 * @param previousLatitude
 * @param previousLongitude
 * @return
 */
private boolean isDistanceAccountable(double currentLatitude,
        double currentLongitude, double previousLatitude,
        double previousLongitude) {

    distance = new float[1];
    if (previousLatitude > 0 && previousLongitude > 0) {

        Location.distanceBetween(currentLatitude, currentLongitude,
                previousLatitude, previousLongitude, distance);

        Log.v(TAG + " > isDistanceAccountable()", "previousLatitude = "
                + previousLatitude + "previousLongitude = "
                + previousLongitude + " currentLatitude =  "
                + currentLatitude + " , currentLongitude = "
                + currentLongitude);
    } else {
        Log.v(TAG + " > isDistanceAccountable()", "previousLatitude = "
                + previousLatitude + "previousLongitude = "
                + previousLongitude);
        return true;
    }

    if (distance[0] > Constants.MINIMUM_DISTANCE_ACCOUNTABLE) {

        Log.v(TAG + " > isDistanceAccountable()", "Distance = "
                + distance[0] + "return true");
        return true;
    } else {

        Log.v(TAG + " > isDistanceAccountable()", "Distance = "
                + distance[0] + "return false");
        return false;
    }

}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

public String getTime() {
    SimpleDateFormat mDateFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
    return mDateFormat.format(new Date());
}

public void appendLog(String text, String filename) {
    File logFile = new File(filename);
    if (!logFile.exists()) {
        try {
            logFile.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        // BufferedWriter for performance, true to set append to file flag
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
                true));
        buf.append(text);
        buf.newLine();
        buf.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void onDestroy() {
    try {
        // Turn off the request flag
        mInProgress = false;
        if (servicesAvailable && mLocationClient != null) {
            mLocationClient.removeLocationUpdates(this);
            // Destroy the current location client
            mLocationClient = null;
        }

        appendLog(DateFormat.getDateTimeInstance().format(new Date())
                + ": Stopped", Constants.LOG_FILE);
    } catch (Exception e) {
        Log.e(TAG + " > onDestroy()", e.toString());
    }
    super.onDestroy();
}

/*
 * Called by Location Services when the request to connect the client
 * finishes successfully. At this point, you can request the current
 * location or start periodic updates
 */
@Override
public void onConnected(Bundle bundle) {

    // Request location updates using static settings
    mLocationClient.requestLocationUpdates(mLocationRequest, this);
    appendLog(DateFormat.getDateTimeInstance().format(new Date())
            + ": Connected", Constants.LOG_FILE);

}

/*
 * Called by Location Services if the connection to the location client
 * drops because of an error.
 */
@Override
public void onDisconnected() {
    // Turn off the request flag
    mInProgress = false;
    // Destroy the current location client
    mLocationClient = null;

    appendLog(DateFormat.getDateTimeInstance().format(new Date())
            + ": Disconnected", Constants.LOG_FILE);
}

/*
 * Called by Location Services if the attempt to Location Services fails.
 */
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    mInProgress = false;

    /*
     * Google Play services can resolve some errors it detects. If the error
     * has a resolution, try sending an Intent to start a Google Play
     * services activity that can resolve error.
     */
    if (connectionResult.hasResolution()) {

        // If no resolution is available, display an error dialog
    } else {

    }
}

}

这是支持班。

public final class Constants {

// Milliseconds per second
private static final int MILLISECONDS_PER_SECOND = 100;
// Update frequency in seconds
private static final int UPDATE_INTERVAL_IN_SECONDS = 10;
// Update frequency in milliseconds
public static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND
        * UPDATE_INTERVAL_IN_SECONDS;
// The fastest update frequency, in seconds
private static final int FASTEST_INTERVAL_IN_SECONDS = 10;
// A fast frequency ceiling in milliseconds
public static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND
        * FASTEST_INTERVAL_IN_SECONDS;
// Stores the lat / long pairs in a text file
public static final String LOCATION_FILE = "sdcard/location.txt";
// Stores the connect / disconnect data in a text file
public static final String LOG_FILE = "sdcard/log.txt";
// Minimum accountable distance in meters
public static final float MINIMUM_DISTANCE_ACCOUNTABLE = 50;

/**
 * Suppress default constructor for noninstantiability
 */
private Constants() {
    throw new AssertionError();
}

}