My Android GPS application do not work even when I manually send latitude and longitude values via Eclipse

StackOverflow https://stackoverflow.com/questions/5816170

문제

I am developing an Android application that gets your current GPS location but the toast does not appear onLocationChanged when I manually send latitude and longitude values via the Location Controls on Eclipse.

Is there anything I have left out, because I have tried all I can think of. I will greatly appreciate your help.

Here is my code:

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) {}
    }


}

Here is my manifest code:

<?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>
도움이 되었습니까?

해결책

You are Listening from NETWORK_PROVIDER not from GPS_PROVIDER. If you want GPS Latitude and Longitude just replace this line.

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

with

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

다른 팁

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) {

}
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top