Senden der GPS-Daten während der App läuft im hintergrund in Android

Ich app entwickelt, die erste GPS-Daten und senden Sie an Server. Ich will also GPS Daten, während meine app läuft im hintergrund in Android-Gerät. Ich Suche in vielen websites, aber ich konnte nicht Antworten.

Verwende ich 2. Klasse. Einer von Ihnen ist der Main-Klasse und ein weiteres ist GPSTracker Klasse. Jetzt gebe ich einen Teil code, der die 2. Klasse.

Main-Klasse

public class Request extends Activity {
public static Float longitude;
public static Float latitude;
public static int ID;  
public static String URL="http://xxx.xxx";
GPSTracker gps;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_request);

final Button button_send_info =    (Button)findViewById(R.id.button_send_info); 
    final TextView show_infos = (TextView) findViewById (R.id.textView1);
    gps = new GPSTracker(Request.this);

    if(gps.canGetLocation()){

        latitude = (float) gps.getLatitude();
        longitude = (float) gps.getLongitude();

        //\n is for new line
        Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();    
    }else{
        //can't get location
        //GPS or Network is not enabled
        //Ask user to enable GPS/network in settings
        gps.showSettingsAlert();
    }

    if( Build.VERSION.SDK_INT >= 8){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 
    }

    button_send_info.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("latitude", latitude.toString()));
            postParameters.add(new BasicNameValuePair("longitude", longitude.toString()));

            String response = null;
            try 
            {
                response = CustomHttpClient.executeHttpPost(URL, postParameters);
                String res=response.toString();
                show_infos.setText(res);
            } 
            catch (Exception e) 
            {
                //hata alanı
            }
        }
    });     

GPSTracker Klasse;

  public class GPSTracker extends Service implements LocationListener {

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

Wie kann ich es tun?

InformationsquelleAutor efrahimates | 2013-03-29

Schreibe einen Kommentar