有三种LocationProvider可以使用
新建一个地图项目。
在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"
android:background="#3399ff">
<ListView
android:id="@+id/alldata"
android:layout_margin="8dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
在MyGPSDemo.java中:
package com.li.gprs;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MyGPSDemo extends Activity {
private ListView allData = null;
private LocationManager locationManager = null;
private ListAdapter adapter = null ;
private List<String> allProviders = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.allData = (ListView) super.findViewById(R.id.alldata);
this.locationManager = (LocationManager) super
.getSystemService(Context.LOCATION_SERVICE);
this.listProviders() ;
}
private void listProviders(){ // 列出全部的数据提供者
this.allProviders = this.locationManager.getAllProviders();
this.adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, this.allProviders);
this.allData.setAdapter(this.adapter) ;
}
}
在AndroidManifest.xml中修改权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.gprs"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyGPSDemo"
android:label="@string/title_activity_my_gpsdemo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>