我想每5分钟将GPS位置发送到服务器,
即使我的应用程序关闭了。
我了解AlertManager可以执行此操作。
设置LocationMaanger与相同的效率明智吗
minTime = 5分钟
minGPS_delta = 0
通常,您什么时候使用:
1)View.postDelay();
2)LocationMaanger.setListener(),minTime = 5分钟,minGPS_delta = 0
3)AlertManager
4)计时器
最佳答案
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.provider.Telephony;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class MyService extends Service
{
private static final String TAG = "BOOMBOOMTESTGPS";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 300000;
private static final float LOCATION_DISTANCE = 328.08f;
String sLat;
String sLong;
String Speed;
String sAlt;
String sTime;
String IMEI;
String Phone;
String LocationCode;
String localTime;
private class LocationListener implements android.location.LocationListener {
Location mLastLocation;
public LocationListener(String provider) {
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-7:00"));
Date currentLocalTime = cal.getTime();
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date.setTimeZone(TimeZone.getTimeZone("GMT-7:00"));
localTime = date.format(currentLocalTime);
sLat = Double.toString(location.getLatitude());
sLong = Double.toString(location.getLongitude());
Speed = Double.toString(location.getSpeed());
sAlt = Double.toString(location.getAltitude());
sTime = Double.toString(location.getTime());
Log.e(TAG, "onLocationChanged: " + location + localTime);
mLastLocation.set(location);
ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(5);
nameValuePair.add(new BasicNameValuePair("Latitude", sLat));
nameValuePair.add(new BasicNameValuePair("Longitude", sLong));
nameValuePair.add(new BasicNameValuePair("IMEI", IMEI));
nameValuePair.add(new BasicNameValuePair("ReportDate", localTime));
nameValuePair.add(new BasicNameValuePair("Altitude", sAlt));
String res;
String loc = URLEncoder.encode(IMEI);
try {
res = CustomHttpClient.sendData("url", nameValuePair);
String resp = res.toString();
resp = resp.replaceAll("\\s+", "");
Log.i(TAG, sLat + " , " + sLong + " , " + resp +" , "+ sAlt);
//Toast.makeText(getApplicationContext(),"Coordenadas:"+sLat+", "+sLong+" Device IMEI: "+IMEI+" resp "+resp,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[]{
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Phone = preferences.getString("Phone", "");
LocationCode = preferences.getString("LocationCode", "");
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
TelephonyManager mngr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
IMEI = mngr.getDeviceId();
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
}
抱歉回复晚了...