android定位服务:查看位置源及其属性

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".LocationProviderActivity" >

    <TextView
        android:id="@+id/location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:textSize="30dp"/>

</RelativeLayout>

LocationProviderActivity.java:

package com.example.demolocation;

import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.location.Criteria;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class LocationProviderActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		StringBuilder sb = new StringBuilder();
		LocationManager manager = (LocationManager)getSystemService(LOCATION_SERVICE);
		
		List<String> providers = manager.getAllProviders();
		String str = "";
		
		LocationProvider provider = null;
		for(Iterator<String> it = providers.iterator();it.hasNext();){
			str = it.next();
			sb.append("位置源:"+str+"\n精度:");
			provider = manager.getProvider(str);
			switch(provider.getAccuracy()){
			case Criteria.ACCURACY_COARSE:
				sb.append("中等精度");
				break;
			case Criteria.ACCURACY_FINE:
				sb.append("低等精度");
				break;
			case Criteria.ACCURACY_HIGH:
				sb.append("高等精度");
				break;
			}
			sb.append("\n耗电量:");
			
			switch(provider.getPowerRequirement()){
			case Criteria.POWER_HIGH:
				sb.append("高耗电量");
				break;
			case Criteria.POWER_MEDIUM:
				sb.append("中耗电量");
				break;
			case Criteria.POWER_LOW:
				sb.append("低耗电量");
				break;
			}
			sb.append("\n-------------\n");
		}
		
		TextView text = (TextView)findViewById(R.id.location);
		text.setText(sb.toString());
	}


}

在 android配置文件中,增加android.permission.ACCESS_FINE_LOCATION权限:

AndroidManifest.xml:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.demolocation.LocationProviderActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>


你可能感兴趣的:(android定位服务:查看位置源及其属性)