Using java Syntax Highlighting
- 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();
- }
- }
Parsed in 0.065 seconds, using GeSHi 1.0.8.4



