Preference this with this is my first android program and I have 3
problems... the code works below to a point ... what I am trying to do
is to send location to a mysql data base every 30 seconds or if the
sensor(accelerometer moves more than 3 meters). I'm trying to save
battery life so it appears from my reading that the sensor class has a
timer which I set to normal SENSOR_DELAY_NORMAL... what is happening
is the API it is overwhelming my database sending information it seems
like every second I only need it like 30 to 45 seconds tops and maybe
not that often... also when I loaded it on my phone after a few
seconds it would freeze and says that it stopped working... 1) why is
it sending every second and how do I change that 2) if I just use the
accelerometer can I simply send meaning update location whenever the
phone moves more than a meter to save battery life or a timed
send(Location update) or maybe every 15 minute if the phone hasn't
updated because the sensor hasn't moved ... is this possible and how
would I do that.... Also I'm pretty sure the problem the phone is
having is with the constant sending of data but are there other issues
I should be mindful of ...
[syntax="java"]package com.javacodegeeks.android.lbs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
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.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.TextView.BufferType;
import android.widget.Toast;
public class LbsGeocodingActivity extends Activity implements
OnClickListener, SensorEventListener {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 3; //
in
// Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 30000; // in
// Milliseconds
protected LocationManager locationManager;
protected Button retrieveLocationButton;
protected Button resumebutton;
protected Button pausebutton;
protected Button quitbutton;
public double longi;
public double lati;
TextView question, test, pwrd, longitudinal, lititudinal;
static TextView connecting;
SensorManager sm;
String gotBread, teat, lockerroom, spotX, spotY;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
retrieveLocationButton = (Button)
findViewById(R.id.retrieve_location_button);
resumebutton =(Button)findViewById(R.id.bResume);
pausebutton=(Button)findViewById(R.id.bPause);
quitbutton=(Button)findViewById(R.id.bQuit);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new
MyLocationListener());
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
Sensor s =
sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this, s,
SensorManager.SENSOR_DELAY_NORMAL);
intialize();
Bundle gotBasket = getIntent().getExtras();
Bundle teatime = getIntent().getExtras();
gotBread = gotBasket.getString("key");
teat = teatime.getString("lock");
lockerroom = teatime.getString("combo");
question.setText(gotBread);
pwrd.setText(teat);
test.setText(lockerroom);
retrieveLocationButton.setOnClickListener(this);
resumebutton.setOnClickListener(this);
pausebutton.setOnClickListener(this);
quitbutton.setOnClickListener(this);
}
}
@Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.retrieve_location_button:
showCurrentLocation();
break;
case R.id.bPause:
onPause();
break;
case R.id.bQuit:
onDestroy();
break;
case R.id.bResume:
onResume();
break;
}
// TODO Auto-generated method stub
}
protected void showCurrentLocation() {
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n
Latitude: %2$s",
location.getLongitude(),
location.getLatitude());
longi = location.getLongitude();
lati = location.getLatitude();
lititudinal.setText("Lat " + lati);
longitudinal.setText("Log " + longi);
// String secondDouble = Double.toString(value);
spotX = Double.toString(lati);
spotY = Double.toString(longi);
putServerData(gotBread, teat, lockerroom, spotX, spotY);
Toast.makeText(LbsGeocodingActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n
Latitude: %2$s",
location.getLongitude(),
location.getLatitude());
longi = location.getLongitude();
lati = location.getLatitude();
spotX = Double.toString(lati);
spotY = Double.toString(longi);
putServerData(gotBread, teat, lockerroom, spotX, spotY);
Toast.makeText(LbsGeocodingActivity.this,
message,Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {Location
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
//String message = String.format(
// "Current Location \n Longitude: %1$s \n
Latitude: %2$s",
// location.getLongitude(),
location.getLatitude());
longi = location.getLongitude();
lati = location.getLatitude();
lititudinal.setText("Lat " + lati);
longitudinal.setText("Log " + longi);
// String secondDouble = Double.toString(value);
spotX = Double.toString(lati);
spotY = Double.toString(longi);
putServerData(gotBread, teat, lockerroom, spotX, spotY);
showCurrentLocation();
//Toast.makeText(LbsGeocodingActivity.this, message,
// Toast.LENGTH_LONG).show();
}
}
public void onProviderDisabled(String s) {
Toast.makeText(LbsGeocodingActivity.this,
"Provider disabled by the user. GPS
turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(LbsGeocodingActivity.this,
"Provider enabled by the user. GPS
turned on",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onResume(){
super.onResume();
sm.registerListener(this,
sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
sm.SENSOR_DELAY_NORMAL);
showCurrentLocation();
}
@Override
public void onPause(){
sm.unregisterListener(this);
super.onPause();
}
@Override
public void onDestroy(){
super.onDestroy();
}
public void intialize() {
question = (TextView) findViewById(R.id.tvMessage);
pwrd = (TextView) findViewById(R.id.tvPassworded);
test = (TextView) findViewById(R.id.tVLast);
connecting = (TextView) findViewById(R.id.tvconnecting);
lititudinal = (TextView) findViewById(R.id.tvLat);
longitudinal = (TextView) findViewById(R.id.tvLong);
}
public void putServerData(String gotBread, String teat, String
lockerroom,
String spotX, String spotY) {
String db_url = "http://www.semsnation.com/loaddata.php";
InputStream is = null;
ArrayList<NameValuePair> request = new
ArrayList<NameValuePair>();
request.add(new BasicNameValuePair("User", gotBread));
request.add(new BasicNameValuePair("Password1", teat));
request.add(new BasicNameValuePair("Group1", lockerroom));
request.add(new BasicNameValuePair("Latitude", spotX));
request.add(new BasicNameValuePair("Longitude", spotY));
try {
HttpClient httpclient = new DefaultHttpClient();
Log.e("log_tag", "Got this far " + httpclient);
HttpPost httppost = new HttpPost(db_url);
httppost.setEntity(new UrlEncodedFormEntity(request));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
String s = convertStreamToString(is);
connecting.setText("got this far " + response);
Log.e("log_tag", "Result: " + s);
} catch (Exception e) {
e.printStackTrace();
connecting.setText("error" + e.toString());
Log.e("log_tag", "Error: " + e.toString());
}// catch
}// send
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}// catch
}// finally
return sb.toString();
}// convertstreamtostring
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
showCurrentLocation();
}
}[/syntax]
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en