GpsStatus.Listener is only registered if GPS is enabled

I have a widget that displays the current location of the user using the LocationManager. I save battery by disabling the local mailing list manager to update the location via the system mailing manager which indicates if the screen is on. To accompany the battery saving efforts, I am trying to implement a GpsStatus.Listener to tell me if gps is enabled or disabled so I can use it in my location manager. Everything works fine if I load the widget ONLY IF GPS is enabled. GpsStatus.Listener not triggered if widget is loaded when GPS is disabled. Obviously there is a workaround for imperefct (loading with gps), but I'm curious and ask why the listener doesn't work if gps is disabled at runtime. I am using os 4.0.3 and my manifest includes exact location. that.

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                     int[] appWidgetIds)
{
    // Get all ids
    thisWidget = new ComponentName(context, MyWidgetProvider.class);
    this.appWidgetManager = appWidgetManager;
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
    remoteViews.setTextViewText(R.id.update, "running......");
    appWidgetManager.updateAppWidget(thisWidget, remoteViews);

    ServicesManager = new MyLocationManager(context);
    streetAddress = new CellAddress(context, ServicesManager);
    ServicesManager.lm.addGpsStatusListener(myGPSListener);

    filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    context.getApplicationContext().registerReceiver(mReceiver, filter);


    LocalBroadcastManager.getInstance(context).registerReceiver(receiveUpdatedLocation, 
            new IntentFilter("sendUpdatedLocation"));


    if (!(ServicesManager.isInternet()))
    {
        remoteViews.setTextViewText(R.id.update, "Address not available" + "\n\n" +
                                    "Check data connection....");
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
    }
    else
    {
        remoteViews.setTextViewText(R.id.update, streetAddress.address$);
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
    }

    Log.d(TAG, "onUpdate");
}

GpsStatus.Listener myGPSListener = new GpsStatus.Listener() 
{

    public void onGpsStatusChanged(int event) 
    {
        if(event == GpsStatus.GPS_EVENT_STARTED)
        {
        Log.d(TAG, "GPS Started");  
        } else if(event == GpsStatus.GPS_EVENT_STOPPED) {
        Log.d(TAG, "GPS Stopped");
        }
    }

};

      

+3


source to share





All Articles