我将使用下面的代码获取Android设备的经度和经度,但我希望每15分钟更新一次纬度和经度。我怎么能得到这个?
package com.position.position;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
StringBuilder stringBuilderlatitude = new StringBuilder();
StringBuilder stringBuilderlongitude = new StringBuilder();
public EditText NombreSeconde;
public EditText NombreMetre;
public float ValueNM;
public int i=0;
LocationManager locationManager;
private String locationProvider = LocationManager.GPS_PROVIDER;
float numberMetre;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button BtnValider = (Button)findViewById(R.id.BtnValider);
BtnValider.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
NombreSeconde = (EditText) findViewById(R.id.ValeurSeconde);
EditText NombreMetreEditText = (EditText) findViewById(R.id.ValeurMetre);
numberMetre = Float.valueOf(NombreMetreEditText.getText().toString());
Toast.makeText(getApplicationContext(), "Paramètres enregistrés", Toast.LENGTH_SHORT).show();
}
});
/////////////////////////////////////
String locationContext = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(locationContext);
//majCoordonnes();
locationManager.requestLocationUpdates(locationProvider,100,100,new MajListener());
}
///////////////////////////
//////////////////////////
class MajListener implements android.location.LocationListener {
public void onLocationChanged(Location location) {
stringBuilderlatitude= new StringBuilder();
stringBuilderlongitude=new StringBuilder();
majCoordonnes();
}
public void onProviderDisabled(String provider){
}
public void onProviderEnabled(String provider){
}
public void onStatusChanged(String provider, int status, Bundle extras){
}
};
public void majCoordonnes() {
Toast.makeText(getApplicationContext(), "Mise à jour des coordonnées",200000).show();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Fournisseurs :");
List<String> providers = locationManager.getProviders(true);
// Pour chaque fournisseur ...
for (String provider : providers) {
stringBuilder.append("\n");
stringBuilder.append(provider);
stringBuilder.append(" : ");
// ... on affiche la position.
Location location = locationManager.getLastKnownLocation(provider);
String locationContext = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(locationContext);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
stringBuilder.append(latitude);
stringBuilder.append(", ");
stringBuilder.append(longitude);
} else {
stringBuilder.append("Non déterminée");
}
}
// Log.i("MaPosition", stringBuilder.toString());
TextView t=new TextView(this);
t=(TextView)findViewById(R.id.TextResultat);
t.setText(stringBuilder.toString());
///////////////////////////////////// partie ajoute//////////////////////////
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
stringBuilderlatitude.append(latitude);
stringBuilderlongitude.append(longitude);
String xlatitude=stringBuilderlatitude.toString();
String xLongitude=stringBuilderlongitude.toString();
Log.i("MaPosition","x1="+xlatitude +"x2="+ xLongitude);
//Log.i("MaPosition", " apres append"+stringBuilderlatitude.toString());
String y=stringBuilderlongitude.toString();
stringBuilderlongitude.append(longitude);
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//nameValuePairs.add(new BasicNameValuePair("year","1980"));
try{
Log.i("MaPosition", "Envoi les coordonnes au serveur");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.kbma.site.com/insertGPS.php?latitude="+xlatitude.toString()+"&longitude="+xLongitude);
Toast.makeText(getApplicationContext(), "Coordonnes envoyées au serveur kbma.site.com",200000).show();
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
catch(Exception e){
Log.i("log_tag", "Error in http connection "+e.toString());
}
}
/////////////////////////
/////////////////
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:2)
您已经从系统请求位置更新,只需将频率的时间从100毫秒增加到15分钟。改变这一行:
locationManager.requestLocationUpdates(locationProvider,100,100, new MajListener());
到此:
locationManager.requestLocationUpdates(locationProvider,(15 * 60 * 1000),100, new MajListener());
另请注意,由于您处于活动内部,因此当用户离开活动时您将停止获取回调。
如果您希望它在后台运行(因此当用户导航时不会关闭),您需要将其放入服务而不是活动
答案 1 :(得分:1)
此代码每5秒更新一次位置(15 * 60 * 1000毫秒)或500米:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, (15 * 60 * 1000), 500, locationListener);
LocationListener locationListener = new MyLocationListener();
// ...
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
currentLocation = new GeoPoint(location);
// ...
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}