0

GPS データを取得してサーバーに送信するアプリを開発しました。私のアプリがAndroidデバイスでバックグラウンドで実行されている間にGPSデータを取得したいのです。私は2つのクラスを持っています。それらの 1 つは Main Class で、もう 1 つのクラスは GPSTracker です。アプリに Android サービスを使用したいと考えています。しかし、私はそれを行う方法がわかりません。私の次のコード; メインクラス:

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

0

ここでAsyncTask を使用する と、優れたチュートリアルが見つかります

于 2013-03-29T16:42:27.757 に答える