0 Replies - 411 Views - Last Post: 14 February 2012 - 11:21 AM

#1 aikidoguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-February 12

API it is overwhelming my database sending information

Posted 14 February 2012 - 11:21 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 ...
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();

        }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1