Android Widget text update

25 Views Asked by At

I have trouble updating my widget text, with the current location each 1000 ms, i only get the default text in the widget, not the location, can anyone give a hint.

The permissions in the manifest is correct, and works in the main application where the location is updated and displayed in the layout.

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.pm.PackageManager;
import android.widget.RemoteViews;
import android.location.Location;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;

import dk.seahawk.hamlocator.algorithm.GridAlgorithm;
import dk.seahawk.hamlocator.algorithm.GridAlgorithmInterface;


public class HamLocatorWidget extends AppWidgetProvider {
    private Context context;

    private static final int LOCATION_PERMISSION_REQUEST_CODE = 100;
    private FusedLocationProviderClient fusedLocationClient;
    private LocationCallback locationCallback;
    private int INTERVAL = 1000;   // location refresh rate
    private GridAlgorithmInterface gridAlgorithmInterface;


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

        for (int appWidgetId : appWidgetIds) {
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.ham_locator_widget);

            // Initialize FusedLocationProviderClient
            fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);

            // Initialize Maidenhead algorithm
            gridAlgorithmInterface = new GridAlgorithm();

            // Create and configure LocationCallback
            locationCallback = new LocationCallback() {
                @Override
                public void onLocationResult(@NonNull LocationResult locationResult) {
                    Location location = locationResult.getLastLocation();
                    assert location != null;

                    // Update location parameters
                    String lastLocation = gridAlgorithmInterface.getGridLocation(location);

                    views.setTextViewText(R.id.appwidget_text, lastLocation);
                    appWidgetManager.updateAppWidget(appWidgetId, views);

                    // Stop location updates after obtaining the location
                    stopLocationUpdates();
                }
            };

            // Start requesting location updates
            startLocationUpdates();
        }
    }

    @Override
    public void onEnabled(Context context) {
        startLocationUpdates();
    }

    @Override
    public void onDisabled(Context context) {
        stopLocationUpdates();
    }

    private void startLocationUpdates() {
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LOCATION_PERMISSION_REQUEST_CODE);
        locationRequest.setInterval(INTERVAL); // Update interval in milliseconds

        if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
    }

    private void stopLocationUpdates() {
        if (fusedLocationClient != null && locationCallback != null) {
            fusedLocationClient.removeLocationUpdates(locationCallback);
        }
    }

}

Here is the xml info file

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/app_widget_description"
    android:initialKeyguardLayout="@layout/ham_locator_widget"
    android:initialLayout="@layout/ham_locator_widget"
    android:minWidth="110dp"
    android:minHeight="40dp"

    android:previewLayout="@layout/ham_locator_widget"
    android:targetCellWidth="4"
    android:targetCellHeight="2"
    android:updatePeriodMillis="1000"
    android:widgetCategory="home_screen" />

0

There are 0 best solutions below