Project Name :MyCurrentLocation
Activity Name : MyCurrentLocationActivity.java
Target : 2.1
package : com.myandroid.location;
uses permission android:name="android.permission.ACCESS_FINE_LOCATION" in menifest
MyCurrentLocationActivity.java coding
=============================
package com.myandroid.location;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MyCurrentLocationActivity extends Activity
{
Button btnGps;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnGps=(Button)findViewById(R.id.button1);
btnGps.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* Use the LocationManager class to obtain GPS locations */
System.out.println( "Click ....");
LocationManager mylocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyAApsLocationListener();
mylocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
});
}
/* Class My Location Listener */
public class MyAApsLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
Toast.makeText( getApplicationContext(),"My current location is: " +" Latitud = " + loc.getLatitude() +" Longitud = " + loc.getLongitude(),Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
///////////////////////////////////////////////Main.xml / /////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:text="Get GPS" />
</LinearLayout>
////////////////////////////////////////////////////////////////////////////////////////////////
menifest.xml
====================================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myandroid.location"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MyCurrentLocationActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
////////////////////////////////////////////////////////////////////////////////////////
when you run the application you must send the lat / long by
Emulator control
you can get Emulator control from the
menu :
window - >Show view - > Emulator Control
and send manual lat log
Activity Name : MyCurrentLocationActivity.java
Target : 2.1
package : com.myandroid.location;
uses permission android:name="android.permission.ACCESS_FINE_LOCATION" in menifest
MyCurrentLocationActivity.java coding
=============================
package com.myandroid.location;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MyCurrentLocationActivity extends Activity
{
Button btnGps;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnGps=(Button)findViewById(R.id.button1);
btnGps.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* Use the LocationManager class to obtain GPS locations */
System.out.println( "Click ....");
LocationManager mylocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyAApsLocationListener();
mylocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
});
}
/* Class My Location Listener */
public class MyAApsLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
Toast.makeText( getApplicationContext(),"My current location is: " +" Latitud = " + loc.getLatitude() +" Longitud = " + loc.getLongitude(),Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
///////////////////////////////////////////////Main.xml / /////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:text="Get GPS" />
</LinearLayout>
////////////////////////////////////////////////////////////////////////////////////////////////
menifest.xml
====================================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myandroid.location"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MyCurrentLocationActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
////////////////////////////////////////////////////////////////////////////////////////
when you run the application you must send the lat / long by
Emulator control
you can get Emulator control from the
menu :
window - >Show view - > Emulator Control
and send manual lat log