このサービスを使用して、バックグラウンドで自分の場所をローカルホストサーバーに更新しました
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import com.google.android.gcm.GCMRegistrar;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class LocationService extends Service{
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
final LocationManager mlocmag = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
final LocationListener mlocList = new MyLocationList();
final Location loc = mlocmag.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// This method is used to get updated location.
mlocmag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocList);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(criteria, true);
Location mostRecentLocation = lm.getLastKnownLocation(provider);
UpdateWithNewLocation(mostRecentLocation);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
private void UpdateWithNewLocation(final Location loc) {
// TODO Auto-generated method stub
if(loc!= null)
{
final double lat =loc.getLatitude(); // Updated lat
final double Long = loc.getLongitude(); // Updated long
final String regId = GCMRegistrar.getRegistrationId(this);
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
String Latitude=String.valueOf(lat);
String Longitude=String.valueOf(Long);
// define the parameter
postParameters.add(new BasicNameValuePair("lat",Latitude));
postParameters.add(new BasicNameValuePair("lang", Longitude));
postParameters.add(new BasicNameValuePair("regId", regId));
String response = null;
// call executeHttpPost method passing necessary parameters
try {
response = CustomHttpClient.executeHttpPost(
"http://192.168.0.144/location.php", // your ip address if using localhost server
postParameters);
String result=response.toString();
// Toast.makeText(this, result ,Toast.LENGTH_LONG).show();
}catch (Exception e) {
Log.e("log_tag","Error in http connection!!" + e.toString());
}
}
else
{
String latLongStr = "No lat and longitude found";
Toast.makeText(this, "Your location is "+latLongStr ,Toast.LENGTH_LONG).show();
}
}
public class MyLocationList implements LocationListener
{
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
UpdateWithNewLocation(arg0);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"GPS Disable ", Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"GPS enabled", Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}