Question

Je travaille sur un widget qui obtiendra la position GPS actuelle et transmettra cette valeur à la page PHP distante pour obtenir les informations et les afficher dans le widget. C’est ce que j’essaie de faire.

Je rencontre un problème lors de la mise en œuvre de Location Listener pour appWidget. Il ne met pas à jour l'emplacement actuel et affiche le widget initial, à savoir "Chargement du widget" (ici, je mets ce texte)

Pouvons-nous implémenter Location Listener pour AppWidgetProvider?
  Ou
   Pouvez-vous me suggérer la solution possible pour obtenir la position GPS dans App Widget?

J'ai placé toutes les autorisations nécessaires dans le fichier manifeste.
Voici mon extrait de code:

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;
    }

}
}

Merci d'avance ...

Cordialement, Raghavendra K.

Était-ce utile?

La solution

Avez-vous ce problème dans l'émulateur ou sur un périphérique? Si vous êtes sur l'émulateur, assurez-vous d'utiliser DDMS pour définir un emplacement GPS. Toutefois, la solution la plus simple consiste peut-être à exécuter votre code sur un appareil pour voir si vous obtenez les mises à jour GPS de cette façon.

Autres conseils

Le problème que je vois est que vous essayez de vérifier curLocation dans onStart () juste après avoir appelé requestLocationUpdates (). LocationManager.requestLocationUpdates () est asynchrone, ce qui signifie que votre LocationListener ne sera pas rappelé avant le retour de onStart (). Demandez à votre LocationListener de mettre à jour l'interface utilisateur après avoir reçu l'emplacement. N'essayez pas de le faire dans onStart.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top