所需权限:<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
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.widget.TextView;
/**
*
* @author Administrator 获取指定的LocationProvider
*
*/
public class LocationProviderActivity2 extends Activity {
private LocationManager locManager;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locationdemo);
tv=(TextView) findViewById(R.id.tv);
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 方式一:通过名称获取指定的LocationProvider
// LocationProvider provider =
// locManager.getProvider(LocationManager.GPS_PROVIDER);
// 方式二:根据Criteria获取LocationProvider
// Criteria代表过滤的条件,大部分时候,应用程序希望能获得指定条件的LocationProvider
// 这就需要借助LocationManager的
//getBestProvider(Criteria criteria,boolean enabledOnly)或getProviders(Criteria criteria,boolean enabledOnly);
Criteria criteria = new Criteria();
criteria.setCostAllowed(false);// 必须是免费的
criteria.setAltitudeRequired(true);// 能提供高度信息
criteria.setBearingRequired(true);// 能提供方向信息
List
providers=locManager.getProviders(criteria,true);
for(int i=0;i