Domanda

Sto lavorando a un Widget che otterrà la posizione GPS corrente e passerà questo valore alla pagina PHP remota per ottenere le Informazioni e visualizzarle nel Widget. Questo è quello che sto cercando di fare.

Sto riscontrando un problema durante l'implementazione del Listener di posizione per appWidget. Non si aggiorna con la posizione corrente e mostra il widget iniziale, ovvero "Caricamento widget" (Qui ho inserito questo testo)

Possiamo implementare Listener di posizione per AppWidgetProvider?
  o
   Puoi suggerirmi la possibile soluzione per ottenere la posizione GPS in Widget app?

Ho inserito tutte le autorizzazioni necessarie nel file manifest.
Ecco il mio frammento di codice:

public class test extends AppWidgetProvider {
static LocationManager locMan;
static Location curLocation;
static Boolean locationChanged;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
    // To prevent any ANR timeouts, we perform the update in a service
    context.startService(new Intent(context, UpdateService.class));
}

public static class UpdateService extends Service {
    // GPS Listener Class
    LocationListener gpsListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Log.w("GPS", "Started");
            if (curLocation == null) {
                curLocation = location;
                locationChanged = true;
            }

            if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude())
                locationChanged = false;
            else
                locationChanged = true;

            curLocation = location;

            if (locationChanged) 
                locMan.removeUpdates(gpsListener);

        }

        public void onProviderDisabled(String provider) {

        }

        public void onProviderEnabled(String provider) {
            // Log.w("GPS", "Location changed", null);
        }

        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            if (status == 0)// UnAvailable
            {

            } else if (status == 1)// Trying to Connect
            {

            } else if (status == 2) {// Available

            }
        }

    };

    // In service start method, I am registering for GPS Updates
    @Override
    public void onStart(Intent intent, int startId) {

        locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000, 1, gpsListener);
        } else {
            this.startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS"));
        }

        if (curLocation != null) {
            double lat = curLocation.getLatitude();
            double lng = curLocation.getLongitude();
            Toast.makeText(getBaseContext(),"Lat : " + String.valueOf(lat) + "\n Long : "+ String.valueOf(lng), Toast.LENGTH_LONG).show();

        }
        // Build the widget update for today
        RemoteViews updateViews = buildUpdate(this);

        // Push update for this widget to the home screen
        ComponentName thisWidget = new ComponentName(this,InKakinadaWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, updateViews);

    }

    public RemoteViews buildUpdate(Context context) {
        // Here I am updating the remoteview
        return updateViews;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // We don't need to bind to this service
        return null;
    }

}
}

Grazie in anticipo ...

Cordiali saluti, Raghavendra K.

È stato utile?

Soluzione

Stai riscontrando questo problema nell'emulatore o su un dispositivo? Se sull'emulatore, assicurati di utilizzare DDMS per impostare una posizione GPS. Ma la cosa più semplice da fare potrebbe essere eseguire il codice su un dispositivo per vedere se ricevi aggiornamenti GPS in quel modo.

Altri suggerimenti

Il problema che vedo è che stai provando a controllare curLocation in onStart () subito dopo aver chiamato requestLocationUpdates (). LocationManager.requestLocationUpdates () è asincrono, il che significa che LocationListener non verrà richiamato fino a quando non verrà restituito onStart (). Chiedi a LocationListener di aggiornare l'interfaccia utente dopo aver ricevuto la posizione, non provare a farlo su onStart.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top