Pergunta

I'm learning about widget now and i have one question. The problem is that string temp is always the same. Never changes. In my view, the variable temp should include latitude, but it still shows the same thing which is initialized at the beginning Where is problem?

    package com.example.applicationgps;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.RemoteViews;
import android.widget.TextView;

public class MyWidgetProvider extends AppWidgetProvider {

    LocationManager locManager; 
    LocationListener locationListener;
    String temp = "Waiting..";


    @Override
    public void onEnabled(Context context) {
        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        super.onEnabled(context);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) { 
                test(location);
            } 

            @Override
            public void onProviderDisabled(String arg0) {
                // TODO Auto-generated method stub  
            }

            @Override
            public void onProviderEnabled(String arg0) {
                // TODO Auto-generated method stub  
            }

            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                // TODO Auto-generated method stub  
            }
        };

        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_demo);
        remoteViews.setTextViewText(R.id.textView1, temp);
        AppLog.logString(temp);
        pushWidgetUpdate(context, remoteViews);
        super.onUpdate(context, appWidgetManager, appWidgetIds);

    }

    public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
        ComponentName myWidget = new ComponentName(context, MyWidgetProvider.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(myWidget, remoteViews);     
    }

    void test(Location location){
        //AppLog.logString(Double.toString(location.getLatitude()));
        String s = Double.toString(location.getLatitude());
        this.temp = s;
        //AppLog.logString(temp);
    }

}
Foi útil?

Solução

Never use member variables in an AppWidgetProvider.

A AppWidgetProvider object has a short lifecycle. It is instantiated by Android for a function call when needed and destroyed afterwards. That's the reason the temp variable remains empty. The LocationListener sets temp in one instance of AppWidgetProvider and Android calls onUpdate() via a new instance .

In order to keep your widget's data (the location) persistent, use one of the data storage options of Android: Android Data Storage

One a side note: there are some examples on stackoverflow of a location widget.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top