话不多少,直接上代码
package android.bzu.hzp;import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     String serviceString = Context.LOCATION_SERVICE;
     LocationManager locationManager = (LocationManager)getSystemService(serviceString);
    String provider = LocationManager.GPS_PROVIDER;
    Location location = locationManager.getLastKnownLocation(provider);
    getLocationInfo(location);
    locationManager.requestLocationUpdates(provider, 2000, 0, locationListener);
    }
   
    private void getLocationInfo(Location location){
    String latLongInfo;
    TextView locationText = (TextView)findViewById(R.id.tv1);
    if (location != null){
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    latLongInfo = "Lat: " + lat + "\nLong: " + lng;
    }
    else{
    latLongInfo = "No location found";
    }
    locationText.setText("Your Current Position is:\n" + latLongInfo);
    }
    
    private final LocationListener locationListener = new LocationListener(){
    @Override
   
    public void onLocationChanged(Location location) {
    getLocationInfo(location);
    }    @Override
    public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
   
    }    @Override
    public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
   
    }    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
   
    }
   };
   
   }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"
    android:id="@+id/tv1"
    />
</LinearLayout>AndroidManifest.xml中的代码<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="android.bzu.hzp"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="3" />    <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>
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    </application>
</manifest>在虚拟机中运行时提示错误

解决方案 »

  1.   

    虚拟机中的先启动GPS才行也就是在状态栏里有那个GPS图标了
      

  2.   

    就是先在手机里启用GPS 才可以。明白了吗
      

  3.   

    如果是模拟器上 ,你需要在模拟器上添加支持GPS功能,或者通过DBMS 查看模拟器运行的具体信息,看看有什么错误信息
      

  4.   

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>