accelerometer can I simply send meaning update location when

Put your problem here if it does not fit any of the other categories.

accelerometer can I simply send meaning update location when

Postby aikidoguy » Tue Feb 14, 2012 7:13 am

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: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.javacodegeeks.android.lbs;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8.  
  9. import org.apache.http.HttpEntity;
  10. import org.apache.http.HttpResponse;
  11. import org.apache.http.NameValuePair;
  12. import org.apache.http.client.HttpClient;
  13. import org.apache.http.client.entity.UrlEncodedFormEntity;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.impl.client.DefaultHttpClient;
  16. import org.apache.http.message.BasicNameValuePair;
  17.  
  18. import android.app.Activity;
  19. import android.content.Context;
  20. import android.hardware.Sensor;
  21. import android.hardware.SensorEvent;
  22. import android.hardware.SensorEventListener;
  23. import android.hardware.SensorManager;
  24. import android.location.Location;
  25. import android.location.LocationListener;
  26. import android.location.LocationManager;
  27. import android.os.Bundle;
  28. import android.util.Log;
  29. import android.view.View;
  30. import android.view.View.OnClickListener;
  31. import android.widget.Button;
  32. import android.widget.RadioGroup;
  33. import android.widget.RadioGroup.OnCheckedChangeListener;
  34. import android.widget.TextView;
  35. import android.widget.TextView.BufferType;
  36. import android.widget.Toast;
  37.  
  38. public class LbsGeocodingActivity extends Activity implements OnClickListener, SensorEventListener  {
  39.  
  40.         private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 3; // in
  41.                                                                                                                                                 // Meters
  42.         private static final long MINIMUM_TIME_BETWEEN_UPDATES = 30000; // in
  43.                                                                                                                                         // Milliseconds
  44.  
  45.         protected LocationManager locationManager;
  46.  
  47.         protected Button retrieveLocationButton;
  48.         protected Button resumebutton;
  49.         protected Button pausebutton;
  50.         protected Button quitbutton;
  51.  
  52.         public double longi;
  53.         public double lati;
  54.         TextView question, test, pwrd, longitudinal, lititudinal;
  55.         static TextView connecting;
  56.         SensorManager sm;
  57.         String gotBread, teat, lockerroom, spotX, spotY;
  58.  
  59.         @Override
  60.         public void onCreate(Bundle savedInstanceState) {
  61.  
  62.                 super.onCreate(savedInstanceState);
  63.                 setContentView(R.layout.main);
  64.  
  65.                 retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
  66.                 resumebutton =(Button)findViewById(R.id.bResume);
  67.                 pausebutton=(Button)findViewById(R.id.bPause);
  68.                 quitbutton=(Button)findViewById(R.id.bQuit);
  69.  
  70.                 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  71.  
  72.                 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
  73.                                 MINIMUM_TIME_BETWEEN_UPDATES,
  74.                                 MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());
  75.                
  76.                 sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  77.                 if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
  78.                         Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
  79.                         sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
  80.  
  81.                 intialize();
  82.                 Bundle gotBasket = getIntent().getExtras();
  83.                 Bundle teatime = getIntent().getExtras();
  84.                 gotBread = gotBasket.getString("key");
  85.                 teat = teatime.getString("lock");
  86.                 lockerroom = teatime.getString("combo");
  87.                 question.setText(gotBread);
  88.                 pwrd.setText(teat);
  89.                 test.setText(lockerroom);
  90.                 retrieveLocationButton.setOnClickListener(this);
  91.                 resumebutton.setOnClickListener(this);
  92.                 pausebutton.setOnClickListener(this);
  93.                 quitbutton.setOnClickListener(this);
  94.        
  95.                 }
  96.         }
  97.         @Override
  98.         public void onClick(View arg0) {
  99.                 switch(arg0.getId()){
  100.                 case R.id.retrieve_location_button:
  101.                         showCurrentLocation();
  102.                         break;
  103.                 case R.id.bPause:
  104.                         onPause();
  105.                         break;
  106.                 case R.id.bQuit:
  107.                         onDestroy();
  108.                         break;
  109.                 case R.id.bResume:
  110.                         onResume();
  111.                         break;
  112.                        
  113.                 }
  114.                 // TODO Auto-generated method stub
  115.  
  116.         }
  117.  
  118.         protected void showCurrentLocation() {
  119.  
  120.                 Location location = locationManager
  121.                                 .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  122.  
  123.                 if (location != null) {
  124.                         String message = String.format(
  125.                                         "Current Location \n Longitude: %1$s \n Latitude: %2$s",
  126.                                         location.getLongitude(), location.getLatitude());
  127.                         longi = location.getLongitude();
  128.  
  129.                         lati = location.getLatitude();
  130.                         lititudinal.setText("Lat  " + lati);
  131.                         longitudinal.setText("Log  " + longi);
  132.                         // String secondDouble = Double.toString(value);
  133.                         spotX = Double.toString(lati);
  134.                         spotY = Double.toString(longi);
  135.  
  136.                         putServerData(gotBread, teat, lockerroom, spotX, spotY);
  137.  
  138.                         Toast.makeText(LbsGeocodingActivity.this, message,
  139.                                         Toast.LENGTH_LONG).show();
  140.                 }
  141.  
  142.         }
  143.  
  144.         private class MyLocationListener implements LocationListener {
  145.  
  146.                 public void onLocationChanged(Location location) {
  147.                        
  148.                         String message = String.format(
  149.                                         "New Location \n Longitude: %1$s \n Latitude: %2$s",
  150.                                         location.getLongitude(), location.getLatitude());
  151.                         longi = location.getLongitude();
  152.                         lati = location.getLatitude();
  153.                         spotX = Double.toString(lati);
  154.                         spotY = Double.toString(longi);
  155.                         putServerData(gotBread, teat, lockerroom, spotX, spotY);       
  156.                         Toast.makeText(LbsGeocodingActivity.this, message,Toast.LENGTH_LONG).show();
  157.                 }
  158.  
  159.                 public void onStatusChanged(String s, int i, Bundle b) {Location location = locationManager
  160.                                 .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  161.  
  162.                 if (location != null) {
  163.                         //String message = String.format(
  164.                         //              "Current Location \n Longitude: %1$s \n Latitude: %2$s",
  165.                         //              location.getLongitude(), location.getLatitude());
  166.                         longi = location.getLongitude();
  167.  
  168.                         lati = location.getLatitude();
  169.                         lititudinal.setText("Lat  " + lati);
  170.                         longitudinal.setText("Log  " + longi);
  171.                         // String secondDouble = Double.toString(value);
  172.                         spotX = Double.toString(lati);
  173.                         spotY = Double.toString(longi);
  174.  
  175.                         putServerData(gotBread, teat, lockerroom, spotX, spotY);
  176.                         showCurrentLocation();
  177.                         //Toast.makeText(LbsGeocodingActivity.this, message,
  178.                         //              Toast.LENGTH_LONG).show();
  179.                         }
  180.                        
  181.                 }
  182.  
  183.                 public void onProviderDisabled(String s) {
  184.                         Toast.makeText(LbsGeocodingActivity.this,
  185.                                         "Provider disabled by the user. GPS turned off",
  186.                                         Toast.LENGTH_LONG).show();
  187.                 }
  188.  
  189.                 public void onProviderEnabled(String s) {
  190.                         Toast.makeText(LbsGeocodingActivity.this,
  191.                                         "Provider enabled by the user. GPS turned on",
  192.                                         Toast.LENGTH_LONG).show();
  193.                 }
  194.  
  195.         }
  196.  
  197.        
  198.         @Override
  199.         public void onResume(){
  200.                 super.onResume();
  201.                 sm.registerListener(this,
  202.                                 sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
  203.                                 sm.SENSOR_DELAY_NORMAL);
  204.                 showCurrentLocation();
  205.         }
  206.         @Override
  207.         public void onPause(){
  208.                 sm.unregisterListener(this);
  209.                
  210.                 super.onPause();
  211.         }
  212.         @Override
  213.         public void onDestroy(){
  214.                 super.onDestroy();
  215.                
  216.         }
  217.  
  218.         public void intialize() {
  219.                 question = (TextView) findViewById(R.id.tvMessage);
  220.                 pwrd = (TextView) findViewById(R.id.tvPassworded);
  221.                 test = (TextView) findViewById(R.id.tVLast);
  222.                 connecting = (TextView) findViewById(R.id.tvconnecting);
  223.                 lititudinal = (TextView) findViewById(R.id.tvLat);
  224.                 longitudinal = (TextView) findViewById(R.id.tvLong);
  225.         }
  226.  
  227.         public void putServerData(String gotBread, String teat, String lockerroom,
  228.                         String spotX, String spotY) {
  229.                 String db_url = "http://www.semsnation.com/loaddata.php";
  230.  
  231.                
  232.  
  233.                 InputStream is = null;
  234.                 ArrayList<NameValuePair> request = new ArrayList<NameValuePair>();
  235.                 request.add(new BasicNameValuePair("User", gotBread));
  236.                 request.add(new BasicNameValuePair("Password1", teat));
  237.                 request.add(new BasicNameValuePair("Group1", lockerroom));
  238.                 request.add(new BasicNameValuePair("Latitude", spotX));
  239.                 request.add(new BasicNameValuePair("Longitude", spotY));
  240.  
  241.                 try {
  242.                         HttpClient httpclient = new DefaultHttpClient();
  243.  
  244.                         Log.e("log_tag", "Got this far " + httpclient);
  245.  
  246.                         HttpPost httppost = new HttpPost(db_url);
  247.  
  248.                         httppost.setEntity(new UrlEncodedFormEntity(request));
  249.                         HttpResponse response = httpclient.execute(httppost);
  250.                         HttpEntity entity = response.getEntity();
  251.                         is = entity.getContent();
  252.  
  253.                         String s = convertStreamToString(is);
  254.                         connecting.setText("got this far   " + response);
  255.  
  256.                         Log.e("log_tag", "Result: " + s);
  257.  
  258.                 } catch (Exception e) {
  259.                         e.printStackTrace();
  260.                         connecting.setText("error" + e.toString());
  261.                         Log.e("log_tag", "Error: " + e.toString());
  262.  
  263.                 }// catch
  264.         }// send
  265.  
  266.         private String convertStreamToString(InputStream is) {
  267.                 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  268.                 StringBuilder sb = new StringBuilder();
  269.                 String line = null;
  270.                 try {
  271.                         while ((line = reader.readLine()) != null) {
  272.                                 sb.append(line + "\n");
  273.                         }
  274.                 } catch (IOException e) {
  275.                         e.printStackTrace();
  276.                 } finally {
  277.                         try {
  278.                                 is.close();
  279.                         } catch (IOException e) {
  280.                                 e.printStackTrace();
  281.                         }// catch
  282.                 }// finally
  283.                 return sb.toString();
  284.         }// convertstreamtostring
  285.         @Override
  286.         public void onAccuracyChanged(Sensor sensor, int accuracy) {
  287.                 // TODO Auto-generated method stub
  288.                
  289.         }
  290.         @Override
  291.         public void onSensorChanged(SensorEvent event) {
  292.                 // TODO Auto-generated method stub
  293.                 showCurrentLocation();
  294.                
  295.                
  296.         }
  297.  
  298. }
Parsed in 0.078 seconds, using GeSHi 1.0.8.4
aikidoguy
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jan 19, 2012 9:51 am

Top

Re: accelerometer can I simply send meaning update location

Postby Phyll » Tue Feb 14, 2012 5:11 pm

Hi aikidoguy,

I must be missing something. Are you expecting the accelerometer to give you GPS coordinates? What does the accelerometer have to do with what you want to get? Is it the accelerometer which you have set up to occur every thirty seconds or the GPS sensor?

Thanks,

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: accelerometer can I simply send meaning update location

Postby aikidoguy » Tue Feb 14, 2012 5:30 pm

my code already get the gps location... I guess what I'm asking is can I use the accelerometer to trigger and event... can it trigger the device to get the location or refer to the location manager ... My big question I guess is why is the code sending every second when I want it to be sent every 30 second... so can I take the code off of a timer and use the accelerometer to trigger an event when it detects that it has moved 3 meters... and is the a way in case it hasn't move to update the location say every 15 minutes...
aikidoguy
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jan 19, 2012 9:51 am

Re: accelerometer can I simply send meaning update location

Postby Phyll » Tue Feb 14, 2012 10:34 pm

Hi aikidoguy,

The location manager is probably what is updating every three meters and/or every 15 minutes not the accelerometer from what I can tell. That's the GPS isn't it? Doesn't that call your listener and doesn't it send it on to some server somewhere? Does the accelerometer even have a range of 3 meters? I think you already have everything covered with the GPS alone.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: accelerometer can I simply send meaning update location

Postby aikidoguy » Tue Feb 14, 2012 10:46 pm

Thank you ... I can get rid of that accelerometer code.... I will delete and post the results
aikidoguy
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jan 19, 2012 9:51 am

Re: accelerometer can I simply send meaning update location

Postby venkatareddy » Sat Jun 02, 2012 9:35 am

hiii,
how to get the last 50 cell towers details..that means how to get the recently visited places information...?
venkatareddy
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Jun 02, 2012 9:28 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 12 guests