Refresh AppWidget with network request using RemoteView button

snachmsm

tl;dr question:

how to properly refresh widget e.g. every minute making network request in background, reliably working Android 5-13 and according to policy

I have an app widget which needs to be refreshed more often than forced updatePeriodMillis and also should provide self-refresh button. Self interval refreshing is made with AlarmManager and works pretty good and under button click I have same PendingIntent:

    Intent intent = new Intent(REPORT_WIDGET_RUN_UPDATE);
    intent.setClass(context, ReportAppWidgetProvider.class);
    return PendingIntent.getBroadcast(context, 0, intent, 
        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);

On providers side onReceive gets called in both cases and I should do some network now. I'm using Volley, so its trivial, but with context passed to onReceive I'm always getting timeouts...

Few year ago I've started to use Service for this purpose, but starting Android 8 ForegroundService must be used for such case (background work, not running any Activity). I didn't wanted to show "auto-disappearing" notification to often, so I've switched to JobIntentService. It has some nasty rare bug and it's deprecated now, so even if it works pretty good I've decided to move on to WorkManager as suggesting last post under link, also official doc or some articles (e.g. this).

So now my JobIntentService becomes Worker followed by this article, Context in workers contructor works with Volley, network connection is done, feedback-broadcast is sent to provider and... After whole operation I'm getting VERY unwanted system-called android.appwidget.action.APPWIDGET_UPDATE. It's not mine action and it breaks my flow!

In Android 13 I see some suggestion in system logs why I'm getting this call

I/LauncherAppWidgetHostView: App widget created with id: 33
I/LauncherAppWidgetHostView: App widget with id: 33 loaded

So my widget after such refresh operation gets "recreated"... Behavior is exacly same on Android 10, but without above/any logs (different launcher)

My question is: how to properly refresh widget with network request in background, reliably working Android 5-13 and according to policy? Maybe should I stick with sometimes-appearing ForegroundService as starting 13 it will be hidden anyway? As far as I remember there is some few-sec-limit for NOT-showing notification? It would be sufficient for me to make network call and then broadcast response to appwidget provider (call made with 3-4 sec. timeout thrown). Any suggestion, prefferably working example appreciated

user496854

My guess is that you're running into the known WorkManager issue where it "restarts" your widget every time. I originally couldn't figure out what was causing this, but there are several issues reported on Google's Issue Tracker, including this one that I filed. And the workaround top fix this issue is

to create a one-time Worker set to 10 years out, (use setInitialDelay) and set at least one constraint on it

You can see the code in my answer here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How To change the imageview of an appwidget (remoteview) multiple times with a short break between?

Changing a background image of a remoteView button?

Refresh data from a network using LiveData

Network Request after button click with Flutter

How to make a reset/refresh button using JavaScript?

Add a page refresh button by using R Shiny

Button doesn't appear in android appwidget layout

Auto Cancel Custom Notification using RemoteView

Custom Notification using Remoteview not showing after Boot

Web Scraping using network request with nodejs

Using Selenium how to get network request

Using Redux to display a notification in response to a network request

Network error on posting a request using axios

Perform request using specific network interface

Network Error making post request using Axios

Loading an image URL into a Kotlin AppWidget using Glide

Picasso is using data for cached images in an appwidget

Using an AsyncTask to download images into a ListView in an AppWidget

How do I request a refresh token using OIDC

Refresh token using google api returning invalid_request

How to refresh a table row in a JSON request using vanilla JavaScript

How to make a request using jQuery to a django server to refresh infos on page

Request Access Token using refresh token with IdentityModel 4.1.1

How to Keep Button Toggled after Refresh the page using localStorage?

R Shiny: automatically refreshing a main panel without using a refresh button

Using ajax to call a php function on button press then refresh

Using JavaScript to refresh or retrieve current information on button click

An unexpected network error has occurred. The app list cannot be refreshed. Please retry by pressing the Refresh button

Using a send button in a GET request in jQuery