How to let my Webview auto restart every 30 m

Asked

Viewed 83 times

0

I wanted to know how to automatically restart my page every 30 minutes in the background. Who can help, thank you.

Public class tela2 extends AppCompatActivity implementa tela2 {

private Timer t;
private int TimeCounter = 0;
WebView xp1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tela2);

    xp1 = (WebView) findViewById(R.id.xp1);
    xp1.getSettings().setJavaScriptEnabled(true);
    xp1.setFocusable(true);
    xp1.setFocusableInTouchMode(true);
    xp1.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    xp1.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    xp1.getSettings().setDomStorageEnabled(true);
    xp1.getSettings().setDatabaseEnabled(true);
    xp1.getSettings().setAppCacheEnabled(true);
    xp1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    xp1.loadUrl("http://pt.clubcooee.com/client/start");
    xp1.setWebViewClient(new WebViewClient());

    getSupportActionBar().setTitle("pegando xp");
}
public  void  startTimer()
{
    t= new Timer();
    t.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // CODIGO A SER EXECULTADO EM SEGUNDO PLANO A CADA 30 MINUTOS

                }
            });
        }
    },18000000,18000000);

1 answer

1


To make something run in the background, we must use the services (Service) of Android.

public class UpdatePageService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

To start a service, put this line of code in your Activity. It can be on onCreate, for example.

startService(new Intent(this, UpdatePageService.class));

To schedule a task from time to time, it is recommended to use Alarmmanager. Alarmmanager has several ways to be implemented depending on the Android Apis your app will cover.

An example of a service with Alarmmanager:

public class UpdatePageService extends Service {
    private AlarmManager alarmManagerUpdate;

    @Override
    public void onCreate() {
        super.onCreate();
        configureAlarmRefreshPage();
    }

    private void configureAlarmRefreshPage(){
        Calendar alarmHour = Calendar.getInstance();
        //configura alarmHour com hora atual
        alarmHour.setTimeInMillis(System.currentTimeMillis());
        //adiciona 30 minutos com base na hora atual
        alarmHour.set(Calendar.MINUTE, alarmHour.get(Calendar.MINUTE) + 30);

        alarmManagerUpdate = (AlarmManager) getContext().
                          getSystemService(Context.ALARM_SERVICE);

        //verifica a versão do Android para configurar com o método adequado
        if (Build.VERSION.SDK_INT >= 23) {                 
            alarmManagerUpdate.setExactAndAllowWhileIdle(AlarmManager.
            RTC_WAKEUP, alarmHour.getTimeInMillis(),
            PendingIntent operation);
        } else if (Build.VERSION.SDK_INT >= 19) {
            alarmManagerUpdate.setExact(AlarmManager.RTC_WAKEUP, 
            alarmHour.getTimeInMillis(),
            PendingIntent operation);
        } else if (Build.VERSION.SDK_INT >= 15) {
            alarmManagerUpdate.set(AlarmManager.RTC_WAKEUP, 
            alarmHour.getTimeInMillis(),
            PendingIntent operation);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // cancelar todos os alarmes
    }
}

In the third parameter of the alarmManagerUpdate.set() configuration, you need to pass a Pendingintent. Pendingintent will let you know when alarmManagerUpdate is in time to refresh the page. The implementation of Pendingintent depends on the way you want to get the alarm warning.

Documentation Pendingintent

Important note: Be careful about implementing an Alarmmanager, because if implemented incorrectly, it can minimize battery life.

Here are some examples of implementations with Pendingintent

Browser other questions tagged

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