サーバーに保存するために緯度、経度、場所の名前を送信しています...問題は、アプリを最初に実行して上記のデータを1回保存するときです...アプリを閉じると、エミュレーターが実行され、日食も発生しますアプリを再起動すると、上記のデータが2回保存されます.....同様に、アプリを閉じて再起動すると1ずつ増加します...なぜ何度も呼び出されるのかわかりませんrequestLocationUpdates()
...サーバーに送信せずにそれはうまくいきます..これについてのアイデアは私を助けてください...
サーバーへのデータの送信..この関数はsend.javaクラスに実装されています
public String postData(List<? extends NameValuePair> nameValuePairs, String wurl){
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(wurl);
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
InputStream is = response.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(20);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
text = new String(baf.toByteArray());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return text;
}
onlocationchaned関数は、LocationListenerを実装し、onlocationchanged内で上記の関数を呼び出すlocation.javaクラスにあります。
public void onLocationChanged(android.location.Location loc) {
String response;
latitude = loc.getLatitude();
longitude = loc.getLongitude();
// Changing type double to string for sending
lati = Double.toString(latitude);
logi = Double.toString(longitude);
String Text = "My current location is: " +
" Latitude = " + Location.lati +
" Longitude = "+ Location.logi;
Toast.makeText(c, Text , Toast.LENGTH_LONG).show();
Log.d("lat", "Changed");
locationName = getAddress(latitude, longitude);
// Add location related data
List<BasicNameValuePair> locationNameValuePair = new ArrayList<BasicNameValuePair>(4);
locationNameValuePair.add(new BasicNameValuePair("latitude", lati));
locationNameValuePair.add(new BasicNameValuePair("longitude", logi));
locationNameValuePair.add(new BasicNameValuePair("locName", locationName));
response = con.postData(locationNameValuePair, wurl);
Toast.makeText(c, response, Toast.LENGTH_LONG).show();
Log.v("IGA", "Address " + locationName);
Toast.makeText(c, locationName, Toast.LENGTH_LONG).show();
}
public String getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(c, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
//String add = obj.getAddressLine(0);
String add = obj.getThoroughfare();
add = add + ", " + obj.getSubLocality();
add = add + ", " + obj.getLocality();
add = add + ", " + obj.getCountryName();
return add;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show();
}
return "";
}
MapActivityを拡張するmainavtivityのlocationManager
protected void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView view = (MapView) findViewById(R.id.themap);
view.setBuiltInZoomControls(true);
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location mylocation = new Location(MainActivity.this);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mylocation);
}