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:
<?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: