Is Webview in the background possible?

Asked

Viewed 59 times

-2

Good morning friends, I am creating a very simple app, it loads a url in the webview (site url of the tracking getrak) because when there is an occurrence the page plays a sound. My app works perfectly, only after I turn off the screen (2minutes +/-) the webview to. How can I get webview to update this URL even when I leave the app???

I tried everything and it didn’t work, I made a Service with Broadcastreceiver, Alarmmanager and such but still it doesn’t keep working

package com.atualizador.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class myBackgroundProcess extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        MainActivity.atualizar.callOnClick();
    }

}

update is a button that has this code:

atualizar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Timer repeatTask = new Timer();
                repeatTask.scheduleAtFixedRate(new TimerTask() {

                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                sistema.loadUrl("https://sistema.getrak.com/semprealerta/msumario/index/index");
                            }
                        });
                    }
                }, 0, 4000);

            }
        });

1 answer

0

This is Jefferson, baby?
Man, to be honest I’ve never seen a case of webview running in the background and I don’t even think this is the best approach.

For some reasons:

  • The purpose of webview is to show something in the UI;
  • Webview is a component used to open external links and external navigation;
  • The S.O. can quit and "cover" features of your application whenever you want to free up memory;

What I could recommend is that you use a Periodicworkrequest (Workmanager) or Alarmmanager with a repetitive alarm triggering a function that will Sync with your backend and make the app play the sound if necessary.

There are several approaches to doing this natively, you decide! The (combined) tools that can solve your problem are:

I’m not digging in, because I see you’ve already implemented some of them.

Hope I helped, hugs ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.