1

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

我用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 类;

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

我该怎么做?

4

1 回答 1

1

android中有一个叫Service的组件。它提供后台活动。您可以在其中执行后台进程。服务没有 GUI。您可以在那里编写 GPS 获取代码。

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

于 2013-03-29T08:13:14.773 回答