How to detect if x amount of seconds have passed in an activity?

1

I have a cardView that contains an ImageView and I have several images. In the configuration of my apk I put an option that the photos pass automatically every certain amount of time. The problem is that when the configuration returns the time is not updated. How can I tell my activity that I have to change the interval when I change it in settings?

This is the code in the mainActivity

public class MyCountDownTimer extends CountDownTimer {
    public MyCountDownTimer(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onFinish() {
        currentImage = viewPager.getCurrentItem();
        currentImage++;
        if (currentImage > 5)
            currentImage = 0;
        setPictureCard();
        countDownTimer.cancel();
        LoadNextTimeOrCheckCurrent();
    }

    @Override
    public void onTick(long millisUntilFinished) {
    }
}


private void LoadNextTimeOrCheckCurrent()
{
    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("checked", false)) {
        // Actualizar cantidad de items
        String integer = PreferenceManager.getDefaultSharedPreferences(this).getString("time_photos", "30");

        boolean minOrSec; // 0 min , 1 sec

        if (integer.indexOf(" ") == 2) {
            if (integer.substring(3, 4).contains("s"))
                minOrSec = true;
            else
                minOrSec = false;

            integer = integer.substring(0, 2);
        }
        else {
            if(integer.length() == 1) {
                integer = integer.substring(0, 1);
                minOrSec = true;
            }
            else if (integer.substring(2, 3).contains("m"))
                minOrSec = false;
            else
                minOrSec = true;

            integer = integer.substring(0, 1);
        }

        if(minOrSec)
            startTime = Integer.parseInt(integer) * 1000;
        else
            startTime = Integer.parseInt(integer) * 60 * 1000;
        countDownTimer = new MyCountDownTimer(startTime, interval);
        countDownTimer.start();

    }
}

private void setPictureCard() {
    viewPager.setCurrentItem(currentImage);
}
    
asked by Alex Rivas 11.06.2018 в 17:20
source

2 answers

0
  

The problem is that when the configuration return is not updated   the time

I see that you are using a Shared Preference to get the saved interval since in your code you have this:

String integer = PreferenceManager.getDefaultSharedPreferences(this).getString("time_photos", "30");

In case of using a preference layout (an .xml with PreferenceScreen) check that you have the correct% key assigned. The field where you set up your interval should look like this:

android:key="time_photos"

In case of not using a layout of preferences when you set your interval you should save it in the shared preferences as is:

SharedPreferences preferencias = getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferencias.edit();
editor.putString("time_photos", intervalo);
editor.apply();

In case you have it like this, and still do not return anything, I recommend that you debug and check that you are entering the correct flow. See also that you are returning the obtaining of the shared preference.

EDIT

  

have look at the if it returns results, what I want is that it   update the moment I change the time in the configuration   you understand me. for example I put 10 seconds and before I was in 30 that   When you go back, do not delay 30 in changing the photo for later   update to 10 do you understand me?

The problem then is another one. It is not updated because the LoadNextTimeOrCheckCurrent() method that you use to check if you have updated the configuration is in the onfinish() of the countdowntimer. That is, what will check once the countdown is finished .

To solve it is as simple as calling countDownTimer.onfinish() once you return from your configuration . If you use a second activity to manage the configuration, this can be included in the onStart() / onResume() method (try one or the other as you do not know the flow of your application). If on the contrary you use fragments, include it in onAttach() .

    
answered by 18.06.2018 / 16:00
source
0

I recommend putting as an container an element called ViewFliper as the parent element of your images, which will control that your images pass one after another carousel type. Then in your CardView change ImageView by ViewFlipper in the following way:

<ViewFlipper
                android:id="@+id/view_flipper"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/img_inicio"
                    android:scaleType="fitXY"
                    android:contentDescription="@string/app_name"/>
                <ImageView
                    android:contentDescription="@string/app_name"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/flipper_a"
                    android:scaleType="fitXY"/>
                <ImageView
                    android:contentDescription="@string/app_name"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/img_slidera"
                    android:scaleType="fitXY"/>
            </ViewFlipper>

The images within ViewFlipper are the ones that will be happening one after the other every so often, now in your java file write the following:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewFlipper viewFlipper = v.findViewById(R.id.view_flipper);
        //SE ASIGNA DA LA VELOCIDAD CON LA QUE VAN A IR PASANDO LAS IMAGENES
        viewFlipper.setFlipInterval(2000);
        //Esto es opciónal es una animacion de entrada y salia de las imagenes
        viewFlipper.setInAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.slide_in_left));
        viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.slide_out_right));
        viewFlipper.startFlipping();
}
    
answered by 14.06.2018 в 18:20