我正在开发一个Android应用程序来获取您当前的GPS位置,但是当我通过Eclipse上的位置控件手动发送纬度和经度值时,吐司不会出现在LocationChanged上。
我有什么遗漏,因为我已经尝试了所有我能想到的东西。我将非常感谢你的帮助。
这是我的代码:
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>
答案 0 :(得分:0)
您正在从NETWORK_PROVIDER收听而不是从GPS_PROVIDER收听。如果您想要GPS纬度和经度,只需替换此行。
mylocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,locationlistener);
与
mylocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationlistener);
答案 1 :(得分: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) {
}
});