Search code examples
androiduser-interaction

How to play video when there is no action perform on screen for 5 mins in Android?


I want when my app exit, after exit if there is no action perform on screen for 5 minutes a video will play every time, this video id define in my app. Any help would be appreciated. Thankx in advance.

I have the following class but it is not worked fine, how can make the same code as Services ?

public class IdlePhoneState extends Activity {

Handler hl_timeout = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  //  setContentView(R.layout.main);

    try{
        hl_timeout.postDelayed(DoOnTimeOut, 120000); // 2 min
        }catch(Exception e)
        {
            e.printStackTrace();
        }
}

//  Toast
Thread DoOnTimeOut = new Thread() {
    public void run() {
        try{
            Toast.makeText(getApplicationContext(), "System is idle", Toast.LENGTH_LONG).show();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
};

@Override
public void onUserInteraction()
{
    super.onUserInteraction();
    //Remove any previous callback
    try{
    hl_timeout.removeCallbacks(DoOnTimeOut);
    hl_timeout.postDelayed(DoOnTimeOut, 120000);

    System.out.println("ggggggggggggggggggggg");
    Intent intr= new Intent(getApplicationContext(), VideoPlayerActivity.class);
    startActivity(intr);


    }catch(Exception e)
    {
        e.printStackTrace();
    }
}

}


Solution

  • Do like below.

    1. you should set an id on the outer-most element of your layout:

      android:id="@+id/entire_view"

    2. In java file find it like below.

      View view = getViewById(R.id.entire_view);

    3. Write touchlistener code for that root layout.

    view.setOnTouchListener( ... in the touch save the touched time to shared prefereces are something like that. Then compare the saved time with current time. if the difference exceeds five mins then play the video.

    For time duration checking try to use Alarm Manager else use CountDownTimer.