Wednesday 14 December 2011

Show Gps Data

ShowGpsDataActivity.java





package com.android.showgpsdata;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;






import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;


public class ShowGpsDataActivity extends Activity {
private TextView txtData;

public static double lat = 0.0, longi = 0.0, gps_lat = 0.0, gps_long = 0.0,
gps_alt = 0.0;
public static float gps_speed = 0.0f;
long gps_time = 0 ;

private LocationManager locationManager;
private LocationListener locationListener;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        txtData=(TextView)findViewById(R.id.txtData);


        txtData.setTextSize(20);
        txtData.setTextColor(Color.BLACK);
        txtData.setBackgroundColor(Color.WHITE);
        txtData.setText(Html.fromHtml("<strong><b>GPS_Time:-</b><br>"+"\n"+getGpsTime(gps_time)+"<br><br><b>GPS_Latitude:- </b><br>"+gps_lat+"<br><br><b>GPS_Longitude:- </b><br>"+gps_long+"<br><br><b>GPS_Altitude:- </b><br>"+gps_alt+"<br><br><b>GPS_Speed:- </b><br>"+gps_speed+" Meter/Second"));
        try {
// ---use the LocationManager class to obtain GPS locations---


locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);


locationListener = new MyLocationListener();


locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);

} catch (Exception e) {
String s = e.toString();
s = "";
}


    }
   
    private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
gps_time = loc.getTime();
gps_lat = loc.getLatitude();
gps_long = loc.getLongitude();
gps_speed = loc.getSpeed();
gps_alt = loc.getAltitude();

 txtData.setText(Html.fromHtml("<b>GPS_Time:-</b><br>"+"\n"+getGpsTime(gps_time)+"<br><br><b>GPS_Latitude:- </b><br>"+gps_lat+"<br><br><b>GPS_Longitude:- </b><br>"+gps_long+"<br><br><b>GPS_Altitude:- </b><br>"+gps_alt+"<br><br><b>GPS_Speed:- </b><br>"+gps_speed+" Meter/Second"));
} else {
gps_time = 0;
gps_lat = 0.0;
gps_long = 0.0;
gps_speed = 0.0f;
gps_alt = 0.0;
}
}


@Override
public void onProviderDisabled(String provider) {
gps_time = 0;
gps_lat = 0.0;
gps_long = 0.0;
gps_speed = 0.0f;
gps_alt = 0.0;
}


@Override
public void onProviderEnabled(String provider) {


}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
gps_time = 0;
gps_lat = 0.0;
gps_long = 0.0;
gps_speed = 0.0f;
gps_alt = 0.0;
}
}


    public String getGpsTime(long gps_time) {
if (gps_time != 0) {
// gps_time = Math.abs(gps_time - System.currentTimeMillis());


DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss a");


Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(gps_time);
return formatter.format(calendar.getTime());
} else
return String.valueOf(gps_time);
}


}


No comments:

Post a Comment