NETWORK_PROVIDER not providing updated locations

扶醉桌前 提交于 2019-11-27 03:11:16
NumberFour

I finally found a way to solve this problem (see my question here).

In my case on S3 Galaxy Mini there were similar symptoms (no location updates until reboot...) like the ones described above. Location in most cases stopped updating when the device reached low power conditions, but sometimes it just happened even when the phone was fully charged. Obviously this is a problem somewhere in the LocationManager. I managed to bypass LocationManager by using the new Google Play Services API for locations (LocationClient class).

This is probably the reason why the location updates were still working with Google Maps, because Google Maps were using Google Play Services API even before it was made public.

I recommend you to follow this link which shows the way how to use Play Services API to get locations instead of using LocationManager if you encounter this problem.

Im running location updates with Play Services and there were absolutely no problems like this anymore - I keep receiving regular location updates anytime.

Beware that this will require that the user must have Google Play installed on their device, so Kindles and the like will be out of the picture. It also requires a minimum of Android 2.2.

I currently suspect it is caused by not using removeUpdates(listener) on app shutdown or activity ondestroy. I have made sure to implement that and hope it fixes it. I can't think of anything else.

--edit-- This did not fix it unfortunately.

If you are working on new phone you have to check if you have accepted all agreements in google maps app - else you will not receive any updates from NETWORK_PROVIDER.

1) first you check whether you have give the permission that is suggested above. 2) you must sure that internet is working in your handset. 3) please find the below code.

public void getLocationByNetworkProvider()
{       
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 10, new LocationListener() {
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub


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

        }

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

        }

        public void onLocationChanged(Location location) {
         location.getLatitude();                                location.getLongitude();

        }
    });     

}`
Teraiya Mayur

you can use like this for update location.Locationlistener will work for location....

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;

import android.util.Log;

public class MainActivity extends Activity implements LocationListener{

protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat;
String provider;
protected String latitude,longitude;
protected boolean gps_enabled,network_enabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtLat = (TextView) findViewById(R.id.textview1);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

@Override
public void onLocationChanged(Location location) {

txtLat = (TextView) findViewById(R.id.textview1);
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" +      location.getLongitude());

}

@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}


}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!