Monday, January 14, 2013

Take advantage of Android's GPS API | TechRepublic

Takeaway: Android app developer William J. Francis walks you through the most basic of GPS usage scenarios in this tutorial.

Modern smartphones have a host of sensors on them; there are accelerometers, cameras, microphones, and the global positioning system (GPS). I?m not sure how I ever got anywhere before Google Maps.

It?s not difficult to use Android?s GPS API in your own applications.?There are, however, a number of steps you must follow and a few guidelines to keep in mind. This tutorial will walk you through the most basic of GPS usage scenarios. You can follow along with the detailed instructions below or download and import the entire project into Eclipse.

1. Create a new Android project in Eclipse. Target Android 1.6 or higher.?Be sure to rename the startup activity to Main.java and the corresponding layout to main.xml.

2. Android provides several mechanisms for getting a user?s location; these mechanisms include network, passive, and GPS.?You can find a description of all three in the official Android documentation. For the purposes of this tutorial, we will only be using the GPS mechanism. Therefore, in the AndroidManifest.xml file, we will need to add a single permission for ACCESS_FINE_PERMISSION.

AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.authorwjf.quickgpsdemo"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            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>
</manifest>

3. The layout for our demonstration is defined in /res/layout/main.xml.?It is a simple linear layout with two labels and a button stacked vertically.

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="12dip"
        android:gravity="center"
        android:textSize="14sp"
        android:textStyle="bold"
        android:textColor="#0000ff"
        android:text="Quick GPS Demo"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="12dip"
        android:gravity="center"
        android:text="Fine Location = (waiting on gps...)"
        android:id="@+id/locat_label" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="12dip"
        android:layout_gravity="center"
        android:text="GPS Settings"
        android:id="@+id/settings_button"/>
</LinearLayout>

4. The /src Main.java file extends Activity while implementing both an OnClickListener and a LocationListener.?We use the standard Android OnCreate override to wire up both the button and the location manager.?Note the constant MIN_TIME in our class; this is how frequently the GPS is updated. You need to keep this at a reasonable value or you will drain the phone battery very quickly.?For the purpose of the demo I used 60 seconds, but even that might be excessive in a live application.

Main.java

package com.authorwjf.quickgpsdemo;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener, LocationListener {
       private LocationManager locationManager;
       private static final long MIN_TIME = 1 * 60 * 1000; //1 minute
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
       locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME, 0, this);
       findViewById(R.id.settings_button).setOnClickListener(this);
   }
}

5. The on click listener is responsible for invoking the system menu to allow a user to enable or disable the GPS. This is needed because for security reasons an application cannot turn on the GPS without user interaction. If that sounds like a pain it is meant to be. While as a developer the idea of being able to toggle the GPS hardware on and off at will sounds nice, I am also an Android user, and I sure don?t want a rogue app running down my battery or worse uploading my location to a website for thieves alerting the world every time I leave my house.

@Override
public void onClick(View v) {
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
       startActivity(intent);
}

6. Next we will be responding to the location updates.?To keep things simple, we are ignoring the callbacks for location services enabled/disabled; however, you must include them, and the usefulness of maintaining state should be apparent.

@Override
public void onLocationChanged(Location loc) {        int lat = (int) (loc.getLatitude());        int lng = (int) (loc.getLongitude());
       ((TextView)findViewById(R.id.locat_label)).setText
         ("Fine Location = (lat:"+Integer.toString(lat)+", lng:"+Integer.toString(lng)+")");
}
@Override
public void onProviderDisabled(String arg0) {
       // TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
       // TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
       // TODO Auto-generated method stub
}

7. When dealing with hardware especially, it is always a good idea to handle the pause and resume states of the Android lifecycle.

@Override
protected void onResume() {
      super.onResume();
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, 0, this);
}
@Override
protected void onPause() {
      super.onPause();
      locationManager.removeUpdates(this);
}

You can load the application to your device and give it a try (Figure A). Remember you will need to use the button to enable the GPS, and then wait at least 60 seconds for the update to occur.

Figure A

If you are running the demo on an emulator, you will have to open a command prompt and telnet into the AVD to fake a location update.

First type:

telnet local host 5554

and then:

geo fix 2520

There, that wasn?t so bad, was it??Just remember the two golden rules: only a user can enable the GPS, and keep your location access to a minimum to preserve the battery.

Automatically sign up for TechRepublic's App Builder newsletter!

Source: http://www.techrepublic.com/blog/app-builder/take-advantage-of-androids-gps-api/2091

july 4th higgs boson Malware Monday First Row Sports American flag Happy 4th of July 4th Of July Desserts

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.