Get information from LocationProvider

Description

The following code shows how to Get information from LocationProvider.

Example

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.java2s.myapplication3.app" >

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="java2s.com"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.java2s.myapplication3.app.MainActivity"
            android:label="java2s.com"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>

</manifest>

Main layout xml file


<?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:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="5dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/detail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="30dp"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </ScrollView>
</LinearLayout>

Main Activity Java code


package com.java2s.myapplication3.app;
//from w w w.  j a  v a2 s .  c  o  m
import android.app.Activity;
import android.content.Context;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Printer;
import android.util.StringBuilderPrinter;
import android.widget.TextView;

import java.util.ArrayList;

// "network" provider will always return null for getLastKnownLocation
// if settings->location and security->Use wireless networks is NOT CHECKED

public class MainActivity extends Activity {

    private LocationManager locationMgr;

    private TextView title;
    private TextView detail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        title = (TextView) findViewById(R.id.title);
        detail = (TextView) findViewById(R.id.detail);
    }

    @Override
    protected void onResume() {
        super.onResume();

        String providerName = getIntent().getStringExtra("PROVIDER_NAME");
        Location lastLocation = locationMgr.getLastKnownLocation(providerName);
        LocationProvider provider = locationMgr.getProvider(providerName);

        StringBuilder sb = new StringBuilder();

        sb.append("location manager data");
        if (lastLocation != null) {
            sb.append("\n");
            Printer printer = new StringBuilderPrinter(sb);
            lastLocation.dump(printer, "last location: ");
        } else {
            sb.append("\nlast location: null\n");
        }
        sb.append("\nprovider properties");
        sb.append("\naccuracy: " + provider.getAccuracy());
        sb.append("\npower requirement: " + provider.getPowerRequirement());
        sb.append("\nhas monetary cost: " + provider.hasMonetaryCost());
        sb.append("\nsupports altitude: " + provider.supportsAltitude());
        sb.append("\nsupports bearing: " + provider.supportsBearing());
        sb.append("\nsupports speed: " + provider.supportsSpeed());
        sb.append("\nrequires cell: " + provider.requiresCell());
        sb.append("\nrequires network: " + provider.requiresNetwork());

        if (providerName.equalsIgnoreCase(LocationManager.GPS_PROVIDER)) {
            GpsStatus gpsStatus = locationMgr.getGpsStatus(null);
            sb.append("\ngps status");
            sb.append("\ntime to first fix: " + gpsStatus.getTimeToFirstFix());
            sb.append("\nmax satellites: " + gpsStatus.getMaxSatellites());
            ArrayList<GpsSatellite> satellites = new ArrayList<GpsSatellite>();
            for (GpsSatellite satellite : gpsStatus.getSatellites()) {
                satellites.add(satellite);
            }
            sb.append("\ncurrent satellites: " + satellites.size());
            if (satellites.size() > 0) {
                for (GpsSatellite satellite : satellites) {
                    sb.append("\nsatellite: " + satellite.getPrn());
                    sb.append("\n   azimuth " + satellite.getAzimuth());
                    sb.append("\n   elevation " + satellite.getElevation());
                    sb.append("\n   signal to noise ratio " + satellite.getSnr());
                }
            }
        }
        title.setText("Provider: " + providerName);
        detail.setText(sb.toString());
    }
}




















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS