-1

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();
    }
}

}

Khushboo
  • 107
  • 1
  • 2
  • 12

1 Answers1

0

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.

itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31
  • I think this is very lengthy...can u tell me some other way like through services or broadcast receiver – Khushboo Nov 17 '14 at 07:59
  • the method OnUserInteraction(), what and where should i define it ? – Khushboo Nov 17 '14 at 08:01
  • setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_UP){ // Do what you want return true; } return false; } }); – itsrajesh4uguys Nov 17 '14 at 08:06
  • Whats mean by action here? it includes touches also right ? If yes then we have to do in this way only. There is nothing lengthy I hope . Just you have to manage one CountDownTimer thats it – itsrajesh4uguys Nov 17 '14 at 08:09
  • But can you tell me how can i make service of the above code – Khushboo Nov 17 '14 at 09:04
  • Same behaviour.. you have to manage it starting and stopping the service. if touched the screen stop and start the service with next 5 mins. – itsrajesh4uguys Nov 17 '14 at 12:06
  • can we intent an Activity in Services ? – Khushboo Nov 17 '14 at 12:22