0

我正在开发一个获取您当前 GPS 位置的 Android 应用程序,但是当我通过 Eclipse 上的位置控件手动发送纬度和经度值时,toast 不会出现 onLocationChanged。

有什么我遗漏的,因为我已经尝试了所有我能想到的。我将非常感谢您的帮助。

这是我的代码:

package com.currentlocation;

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.Button;
import android.widget.Toast;

public class CurrentLocation extends Activity {
    Button button1;
   LocationManager mylocationManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1=(Button)findViewById(R.id.button1);
        mylocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationlistener=new mylocation();
        mylocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,locationlistener);
    }
    public class mylocation implements LocationListener
    {
        @Override
        public void onLocationChanged(Location location) {
            String mycurrentLocation="My current location is Latitude"+location.getLatitude()+" and Longitude"+location.getLongitude();
            Toast.makeText(getApplicationContext(),mycurrentLocation, Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onProviderDisabled(String provider) {}
        @Override
        public void onProviderEnabled(String provider) {}
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    }


}

这是我的清单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.currentlocation"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CurrentLocation"
                  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" />

</manifest>
4

2 回答 2

0

您正在收听来自 NETWORK_PROVIDER 而不是来自 GPS_PROVIDER。如果你想要 GPS 纬度和经度,只需替换这条线。

 mylocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,locationlistener);

 mylocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationlistener);
于 2011-04-28T09:14:48.663 回答
0
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {

}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
});
于 2014-05-28T12:46:27.083 回答