質問

i am trying to make location app on android when i run the app on emulator and send fake longitude and latitude it show longitude and latitude in logs but when i run it on my phone it does not give me current location cordinates or any output in logs here is my code

    public void getLocation(){

    final LocationManager manager= (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    LocationListener listener= new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub

            loc=new ArrayList<SetLoc>();
            SetLoc vari= new SetLoc();
            double longitude=location.getLongitude();
            double latitude=location.getLatitude();
            String lng=""+longitude;
            String lat=""+latitude;

            Log.d("onLocationChange", lng+" "+lat );

            vari.setlon(lng);
            Log.d("setlon", "setting the longitude"+lng);
            vari.setlat(lat);
            loc.add(vari);


            String url="http://10.16.49.234:8080/WebApplication6/webresources/generic";


            sendcord(lng,lat,url);

            Log.d("onchangelocation", "aftersnd.execute()");

        }
    };
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);


}`

help please

役に立ちましたか?

解決

Your code seem perfect, I assume you are getting Null because there is no previous GPS value in your device. Do this,

Load the application in your device, move to open sky, run the application, wait for 2 minutes. Come back to office inside, and then execute above code

OR

Run the application in open sky..

他のヒント

You're specifically requesting GPS data, are you sure that it is enabled on the device you are testing on?

Have you looked into the fused location provider bundled with Google Play Services? With this provider you can immediately retrieve the last known location and request updates in a battery efficient way as multiple apps communicate with Google Play Services instead of directly with the device's sensors.

http://developer.android.com/google/play-services/location.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top