0

I have a problem trying to implement a popup image when screen of android phone is left idle or unused for 10 seconds. Currently I am using a timer but need to implement an on-idle listener instead but I have no idea how this can be done. Can anybody help me?

The Popup image is done by a timer in the below code.

public class CustomPopupWindowExample extends Activity {
    private Animation animShow, animHide;
    com.example.custompopupwindowexample.TransparentPanel popup;

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);

        setContentView(R.layout.popup);

        popup = (com.example.custompopupwindowexample.TransparentPanel) findViewById(R.id.popup_window);
        //Declare the timer
        Timer t = new Timer();
        //Set the schedule function and rate
        t.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                //Called each time when 1000 milliseconds (1 second) (the period parameter)

                //We must use this function in order to change the text view text
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        popup.setVisibility(View.GONE);
                        initPopup();
                    }                        
                });
            }                    
        },
        //Set how long before to start calling the TimerTask (in milliseconds)
        10000,
        //Set the amount of time between each execution (in milliseconds)
        50000);
    }

    private void initPopup() {
        final com.example.custompopupwindowexample.TransparentPanel popup = (com.example.custompopupwindowexample.TransparentPanel) findViewById(R.id.popup_window);

        animShow = AnimationUtils.loadAnimation(this, R.anim.popup_show);
        animHide = AnimationUtils.loadAnimation(this, R.anim.popup_hide);

        final Button   hideButton = (Button) findViewById(R.id.hide_popup_button);

        final ImageView locationDescription = (ImageView) findViewById(R.id.location_description);

        locationDescription.setBackgroundResource(R.drawable.nike_logo);
        popup.setVisibility(View.VISIBLE);
        popup.startAnimation(animShow);
        hideButton.setEnabled(true);

        hideButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                popup.startAnimation( animHide );
                hideButton.setEnabled(false);
                popup.setVisibility(View.GONE);
        }});
    }
}
gotwo
  • 663
  • 8
  • 16
PhillyT
  • 15
  • 6

1 Answers1

0

you should use a touchlistener so onTouchEvent you can check whether user has touched it or not. Define a variable to hold idle time value, if he/she touches then set that variable to 0, otherwise increment it, and when it reaches 10 seconds(or whichever value you want) display that pop up.

Onur A.
  • 3,007
  • 3
  • 22
  • 37