取得最佳的LocationProvider

取得最佳的LocationProvider_第1张图片

 

 

新建一个地图项目。

 

 

在main.xml中:

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

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

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:background="#3399ff">

  <TextView

     android:id="@+id/fineprovider"

     android:layout_margin="8dp"

     android:layout_width="fill_parent"

     android:layout_height="wrap_content"

     android:textColor="#ffffff"/>

</LinearLayout>

 

 

 

 

 

在MyGPSDemo.java中:

 

package com.li.gprs;

 

import android.app.Activity;

import android.content.Context;

import android.location.Criteria;

import android.location.LocationManager;

import android.os.Bundle;

import android.widget.TextView;

 

public class MyGPSDemo extends Activity {

  private TextView fineProvider = null;

  private LocationManager locationManager = null;

 

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.fineProvider = (TextView) super.findViewById(R.id.fineprovider);

     this.locationManager = (LocationManager) super

         .getSystemService(Context.LOCATION_SERVICE);

     Criteria criteria = new Criteria() ;

     criteria.setAccuracy(Criteria.ACCURACY_FINE) ; // 使用最准确精度

     criteria.setCostAllowed(false); // 不产生费用

     criteria.setPowerRequirement(Criteria.POWER_LOW); // 耗电量低

     String provider = this.locationManager.getBestProvider(criteria, true);

     this.fineProvider.setText("经过匹配,最佳的Provider为:" + provider) ;

  }

}

 

 

 

 

 

在AndroidManifest.xml中修改权限:

 

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

    package="com.li.gprs"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

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

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

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyGPSDemo"

            android:label="@string/title_activity_my_gpsdemo" >

            <intent-filter>

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

 

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

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

你可能感兴趣的:(取得最佳的LocationProvider)