How to let my WebView restart automatically every 30 m

0

I wanted to know how to leave my page automatically restarting every 30 minutes in the background. Anyone 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);
    
asked by anonymous 14.08.2017 / 05:53

1 answer

1

To get something to work in the background, we should 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, place this line of code in your Activity. It could be in onCreate, for example.

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

To schedule a task from time to time, it is recommended to use the AlarmManager. AlarmManager has several ways to deploy 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 inform you when alarmManagerUpdate is in the time to refresh the page. The implementation of PendingIntent depends on the way you want to get the alarm warning.

PendingIntent Documentation

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

Here are some examples of implementations with PendingIntent

    
30.08.2017 / 03:49