应用程序在Android中运行后台时发送GPS数据

时间:2013-03-29 08:04:15

标签: android gps

我开发了获取GPS数据并发送到服务器的应用程序。所以我想获取GPS数据,而我的应用程序在Android设备中运行背景。我在许多网站上搜索过,但我无法回答。

我使用2级。其中一个是Main类,另一个是GPSTracker类。现在我给出一些2类的部分代码。

主类

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

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

我该怎么办?

1 个答案:

答案 0 :(得分:1)

android中有一个名为Service的组件。它提供背景活动。您可以在其中执行后台处理。服务没有GUI。你可以在那里写一个GPS提取代码。

Vogella查看服务组件的简单教程。