Wednesday, September 19, 2012

Location Manager Demo in Android


Hello Friends!
  I am going to share very useful demo for location manager in android. It will search all known location and display in a list. I am sharing code below hope it will helpful for you.

1-Print Screen:

2-manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.location.manager"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".LocationManagerDemoActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 </manifest>

3-LocationManagerDemoActivity.java
package com.manish.location.manager;

import java.util.List;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class LocationManagerDemoActivity extends Activity implements LocationListener {
       private static final String[] States = { "No Service",
                     "Temporarily Unavailable", "Available" };

       private TextView result;
       private LocationManager locationManager;
       private String serviceProvider;

       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);

              // Get the output UI
              result = (TextView) findViewById(R.id.result);

              // Get the location manager
              locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

              // List all providers:
              List<String> providers = locationManager.getAllProviders();
              for (String provider : providers) {
                     printProvider(provider);
              }

              Criteria criteria = new Criteria();
              serviceProvider = locationManager.getBestProvider(criteria, false);
              result.append("\n\nProvider:\n");
              printProvider(serviceProvider);

              result.append("\n\nLocations (starting with last known):");
              Location location = locationManager.getLastKnownLocation(serviceProvider);
              printLocation(location);
       }

       /** Register for the updates when Activity is in foreground */
       @Override
       protected void onResume() {
              super.onResume();
              locationManager.requestLocationUpdates(serviceProvider, 20000, 1, this);
       }

       /** Stop the updates when Activity is paused */
       @Override
       protected void onPause() {
              super.onPause();
              locationManager.removeUpdates(this);
       }

       public void onLocationChanged(Location location) {
              printLocation(location);
       }

       public void onProviderDisabled(String provider) {
              // let okProvider be bestProvider
              // re-register for updates
              result.append("\n\nProvider Disabled: " + provider);
       }

       public void onProviderEnabled(String provider) {
              // is provider better than bestProvider?
              // is yes, bestProvider = provider
              result.append("\n\nProvider Enabled: " + provider);
       }

       public void onStatusChanged(String provider, int status, Bundle extras) {
              result.append("\n\nProvider Status Changed: " + provider + ", Status="
                           + States[status] + ", Extras=" + extras);
       }

       private void printProvider(String provider) {
              LocationProvider info = locationManager.getProvider(provider);
              result.append(info.toString() + "\n\n");
       }

       private void printLocation(Location location) {
              if (location == null)
                     result.append("\nLocation[unknown]\n\n");
              else
                     result.append("\n\n" + location.toString());
       }

}

4-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" >

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/result"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>


No comments:

Post a Comment