首先在Mainifest.xml获取gps设备的权限
不然的话即使你搜索不到bestProvider
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lichen"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
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-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>
布局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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/ScanProvidersButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试当前设备的Providers"/>
<Button
android:id="@+id/bestProviderButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="最好的Provider"/>
</LinearLayout>
package com.lichen;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button scanProvider = null;
private Button bestProvider = null;
private LocationManager locationManager = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//位置管理器
locationManager = (LocationManager) MainActivity.this
.getSystemService(Context.LOCATION_SERVICE);
//两个按钮,一个用来搜索Provider,一个用来搜索最好的Provider
scanProvider = (Button) findViewById(R.id.ScanProvidersButton);
bestProvider = (Button) findViewById(R.id.bestProviderButton);
//遍历Provider,打印到控制台()当中的是一个监听器
scanProvider.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 得到所有的provider,返回的是一个存放String的List
List<String> providers = locationManager.getAllProviders();
System.out.println("providers:");
for (Iterator<String> it = providers.iterator(); it.hasNext();) {
String provider = (String) it.next();
System.out.println("=============" + provider);
}
}
});
//用来输出最好的Provider
bestProvider.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Criteria criteria = new Criteria();
// 设置查找最好的provider的过滤条件
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(false);
// 获得最好的provider,第二个参数表示是否考虑provider可用
String provider = locationManager.getBestProvider(criteria,
false);
System.out.println("best provider ---> " + provider);
}
});
}
}