We provide complete mobile and web apps development solutions

Tuesday, August 21, 2012

GPS – Android


 LocationManager class provides the access to  know the location of the mobile. It will provide the current geo location to the user.
Here no need to instantiate the class directly, instead you can use Context.getSystemService(Context.LOCATION_SERVICE)

Android provides the   android.location package which provides the API to determince the current geo position.

LocationProvider is a super class of different location providers, which  will provide the current geo location.

We can get the location by using three methods,

Network  -  uses mobile network,wifi
Gps      -  uses mobile gps to find the location via satellites.
Passive  - allows to participate for getting location on other components to save the power.

To use the gps service in our android mobile, we need to declare permission in manifest file:


ACCESS_FINE_LOCATION
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/>

If your getting location by gprs we  need to declare
ACCESS_COARSE_LOCATION
android.provider.Settings class provides to change the gps settings

LocationManager service = (LocationManager)getSystemService(LOCATION_SERVICE);
boolean enable = service.isProviderEnabled(LocationManager.GPS_PROVIDER);

We can check the gps enabled  by using if, else conditions
if (!enable)
 enable the gps by going to mobilesettings.

If we use  intents, we can directly goes to gps settings
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
Some constant methods:
public static final String GPS_PROVIDER
name of the gps provider. This will provide the location updates using satellites.
public static final String KEY_PROVIDER_ENABLED
it will hold the Boolean value when a provider enabled/disabled event is broadcast using a Pending Intent.
public static final String PROVIDERS_CHANGED_ACTION
Broadcast intent action when the configured location providers change.


Some public methods:
public GpsStatus getGpsStatus(GpsStatus status)
it will gives the information about current gps status.

Public LocationProvider getProvider (String name)
It gives the information associated with location provider of the given name
public Boolean isProviderEnabled(String provider)
Returns the current enabled/disabled status of the given provider.



public class MainActivity extends Activity implements LocationListener {

    TextView txtInfo;

    LocationManager lom;

    StringBuilder sb;

    int noOfFixes = 0;


    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

       

      //TextView to display gps data

        textdata = (TextView) findViewById(R.id.textView1);

       

        /* the location manager is the most vital part it allows access

         * to location and GPS status services */

        lom = (LocationManager) getSystemService(LOCATION_SERVICE);

    }

   

    @Override

    protected void onResume() {

      

       //  request updates every 1000ms or 10m

        

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100f, this);

        super.onResume();

    }

   

    @Override

    protected void onPause() {

        /* GPS, as it turns out, consumes battery like crazy */

        lm.removeUpdates(this);

        super.onPause();

    }


    public void onLocationChanged(Location location) {

        Log.v(tag, "Location Changed");


        sb = new StringBuilder(512);

       

        noOfFixes++;


        /* display the data in the TextView */


        sb.append("No. of Fixes: ");

        sb.append(noOfFixes);

        sb.append('\n');

        sb.append('\n');


        sb.append("Londitude: ");

        sb.append(location.getLongitude());

        sb.append('\n');


        sb.append("Latitude: ");

        sb.append(location.getLatitude());

        sb.append('\n');


        sb.append("Altitiude: ");

        sb.append(location.getAltitude());

        sb.append('\n');


        sb.append("Accuracy: ");

        sb.append(location.getAccuracy());

        sb.append('\n');


        sb.append("Timestamp: ");

        sb.append(location.getTime());

        sb.append('\n');


        txtInfo.setText(sb.toString());

    }


    public void onProviderDisabled(String provider) {

        /*when the GPS is disabled in settings */

        Log.v(tag, "Disabled");


     //open gps settings

        Intent intent = new Intent(

                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);

        startActivity(intent);

    }


    public void onProviderEnabled(String provider) {

        Log.v(tag, "Enabled");

        Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();


    }


    public void onStatusChanged(String provider, int status, Bundle extras) {

        /*  GPS status change */

        switch (status) {

        case LocationProvider.OUT_OF_SERVICE:

            Log.v(tag, "Status Changed: Not in service");

            Toast.makeText(this, "Status Changed: Not in Service",

                    Toast.LENGTH_SHORT).show();

            break;

        case LocationProvider.TEMPORARILY_UNAVAILABLE:

            Log.v(tag, "Status Changed: Temporarily Unavailable");

            Toast.makeText(this, "Status Changed: Temporarily Unavailable",

                    Toast.LENGTH_SHORT).show();

            break;

        case LocationProvider.AVAILABLE:

            Log.v(tag, "Status Changed: Available");

            Toast.makeText(this, "Status Changed: Available",

                    Toast.LENGTH_SHORT).show();

            break;

        }

    }


    @Override

    protected void onStop() {

        /*saving the state is not important for this  app */

        finish();

        super.onStop();

    }

}




activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >


    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_marginLeft="68dp"

        android:textSize="40sp"

         />


</RelativeLayout>


we need to declare gps permission in manifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.gps"

    android:versionCode="1"

    android:versionName="1.0" >


    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />


    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MainActivity"

            android:label="@string/title_activity_main" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

       

    </application>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

</manifest>


0 coment�rios:

Post a Comment

Online Training

Your Name :
Your Email: (required)
Your Message: (required)

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts