天天看點

Android學習筆記(二十八)

使用者定位

UserLocationActivity

package org.wp.activity;

import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.location.Criteria;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * setAltitudeRequired(boolean altitudeRequired) 
 * 是否提供海拔/高度的資訊 
 * setAccuracy(int accuracy) 
 * 設定經緯度的精确程度 ACCURACY_FINE/ACCURACY_COARSE 
 * setBearingRequired(boolean bearingRequired) 
 * 是否提供方向的資訊 
 * setBearingAccuracy(int accuracy) 
 * 設定方向的精确程度 ACCURACY_LOW/ACCURACY_HIGH/NO_REQUIREMENT 
 * setSpeedRequired(boolean speedRequired) 
 * 是否提供速度的資訊 
 * setSpeedAccuracy(int accuracy) 
 * 設定速度精确度 ACCURACY_LOW/ACCURACY_HIGH/NO_REQUIREMENT 
 * setHorizontalAccuracy(int accuracy)
 * 設定水準精确度 ACCURACY_LOW/ACCURACY_MEDIUM/ACCURACY_HIGH/NO_REQUIREMENT
 * setVerticalAccuracy(int accuracy) 
 * 設定垂直精确度 ACCURACY_LOW/ACCURACY_HIGH/NO_REQUIREMENT 
 * setPowerRequirement(int level)
 * 設定最高耗電等級 POWER_LOW/POWER_MEDIUM/POWER_HIGH/NO_REQUIREMENT
 * setCostAllowed(boolean costAllowed) 
 * 是否允許産生費用
 * 
 * @author wp
 * 
 */
public class UserLocationActivity extends Activity {
	private Button searchProviderButton;
	private Button bestProviderButton;
	private LocationManager locationManager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		searchProviderButton = (Button) this.findViewById(R.id.searchProviderButton);
		bestProviderButton = (Button) this.findViewById(R.id.bestProviderButton);
		locationManager = (LocationManager) UserLocationActivity.this.getSystemService(Context.LOCATION_SERVICE);

		searchProviderButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// 獲得所有的Provider
				List<String> providers = locationManager.getAllProviders();
				String[] strs = new String[providers.size()];
				for (int i = 0; i < providers.size(); i++) {
					strs[i] = providers.get(i);
				}
				AlertDialog.Builder builder = new AlertDialog.Builder(
						UserLocationActivity.this);
				builder.setTitle("所有Provider");
				builder.setItems(strs, null);
				AlertDialog dialog = builder.create();
				dialog.show();
			}
		});

		bestProviderButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Criteria criteria = new Criteria();
				criteria.setAccuracy(Criteria.ACCURACY_COARSE);
				criteria.setBearingRequired(false);
				criteria.setSpeedRequired(false);
				criteria.setPowerRequirement(Criteria.POWER_LOW);
				criteria.setCostAllowed(false);
				// 第二個參數 enabledOnly provider是否是可用狀态
				String provider = locationManager.getBestProvider(criteria,
						false);
				AlertDialog.Builder builder = new AlertDialog.Builder(
						UserLocationActivity.this);
				builder.setTitle("最符合标準的provider");
				builder.setMessage(provider);
				AlertDialog dialog = builder.create();
				dialog.show();
			}
		});
	}
}
           

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"
    >
	<Button
		android:id="@+id/searchProviderButton"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="查詢目前裝置支援的Provider"
		/>
	<Button
		android:id="@+id/bestProviderButton"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="獲得最符合條件的Provider"
		/>	
</LinearLayout>
      

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.wp.activity" android:versionCode="1" android:versionName="1.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".UserLocationActivity" 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>
	<uses-sdk android:minSdkVersion="7" />
	<!-- Allows an application to access fine (e.g., GPS) location -->
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
	<!-- Allows an application to access coarse (e.g., Cell-ID, WiFi) location -->
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>      
上一篇: Wifi廣播狀态