In this program we can calculate the GPS location.First we have to take a XML layout that contains a simple button and a textView widget.When we click the button,it gives us a toast containing the GPS location.Here is the XML code for the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Now it's time for some coding.For this we will use three java class here.
The class MyGps.class is the main class here,where the button click event is defined.
package com.android.gps;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MyGps extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button btn = (Button)findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
gpsLocation gp = new gpsLocation(MyGps.this);
gp.getlocation();
Toast.makeText(MyGps.this, Data.Gps, Toast.LENGTH_LONG).show();
}
});
}
In this code when we click the button the go object of gpsLocation class (that is defined in the next java code) is created.This gp with the getLocation() method(defined in gpsLocation class) fetch the gps location and show it with the toast.Now it's time for the gpsLocation class:
In this code the gpsLocation retrieval method is shown.Also a data class is defined to declare a static string variable.
public class Data {
public static String Gps;
}
In this data.Gps, the value of c with latitude and longitude is saved and used by the toast in MyGps class to show the value.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Now it's time for some coding.For this we will use three java class here.
The class MyGps.class is the main class here,where the button click event is defined.
package com.android.gps;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MyGps extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button btn = (Button)findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
gpsLocation gp = new gpsLocation(MyGps.this);
gp.getlocation();
Toast.makeText(MyGps.this, Data.Gps, Toast.LENGTH_LONG).show();
}
});
}
In this code when we click the button the go object of gpsLocation class (that is defined in the next java code) is created.This gp with the getLocation() method(defined in gpsLocation class) fetch the gps location and show it with the toast.Now it's time for the gpsLocation class:
package com.android.gps;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class gpsLocation {
Context context;
public gpsLocation(Context c)
{
context=c;
}
double LAT=0.0,LONG=0.0;
private LocationManager lm;
private LocationListener locationListener;
public void getlocation()
{
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
class MyLocationListener implements LocationListener
{
String a;
String b;
String c;
public void onLocationChanged(Location loc)
{
if (loc != null) {
LAT = loc.getLatitude();
LONG = loc.getLongitude();
a=Double.toString(LAT);
b=Double.toString(LONG);
c="LAT:"+a+" "+"LONG:"+b;
Data.Gps=c;
}
}
@Override
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
}
}
}
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class gpsLocation {
Context context;
public gpsLocation(Context c)
{
context=c;
}
double LAT=0.0,LONG=0.0;
private LocationManager lm;
private LocationListener locationListener;
public void getlocation()
{
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
class MyLocationListener implements LocationListener
{
String a;
String b;
String c;
public void onLocationChanged(Location loc)
{
if (loc != null) {
LAT = loc.getLatitude();
LONG = loc.getLongitude();
a=Double.toString(LAT);
b=Double.toString(LONG);
c="LAT:"+a+" "+"LONG:"+b;
Data.Gps=c;
}
}
@Override
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
}
}
}
In this code the gpsLocation retrieval method is shown.Also a data class is defined to declare a static string variable.
package com.android.gps;
public class Data {
public static String Gps;
}
In this data.Gps, the value of c with latitude and longitude is saved and used by the toast in MyGps class to show the value.
No comments:
Post a Comment