GpsStatus.Listener works only if GPS is on

后端 未结 1 1981
悲哀的现实
悲哀的现实 2020-12-12 00:19

in my App I have a GpsStatus.Listener to receive events when the user enables or disables GPS. Everything works fine if GPS is on before I start the app. In

相关标签:
1条回答
  • 2020-12-12 00:37

    Well, LocationListener has been provided with:

    onProviderDisabled(),onProviderEnabled() and onStatusChanged() for exactly this purpose.

    GpsStatus.Listener delivers info about GPS service's inner workings. It is not to be used for telling the status pf GPS Provider.

    LocationListener delivers info about providers. The moment you register LocationListener with location provider, onProviderEnabled()/onProviderDisabled() is called accordingly, and your app can always tell when GPS is turned on or off.

    Try this:

    public class test extends Activity implements LocationListener{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,10,this);
        }
    
    
        @Override
        public void onLocationChanged(Location location) {
        }
    
        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
        }
    
        @Override
        public void onProviderEnabled(String s) {
            if(LocationManager.GPS_PROVIDER.equals(s)){
                Toast.makeText(this,"GPS on",Toast.LENGTH_SHORT).show();
            }
        }
    
        @Override
        public void onProviderDisabled(String s) {
            if(LocationManager.GPS_PROVIDER.equals(s)){
                Toast.makeText(this,"GPS off",Toast.LENGTH_SHORT).show();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题