用户定位
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>