Search code examples
androiduser-interaction

android detect user inactivity after key pressed


In my mediaplayer I've added a view that is shown if menu key is pressed, I want it to be hidden after some time i.e user don't want to see the view, The view is horizontal and i want it to hide after some seconds that user won't press right or left key.

I've place my logic here but it didn't worked out: "Similar is my view to be shown"

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        super.onKeyDown(keyCode, event);
        lastUsed = System.currentTimeMillis();



case KeyEvent.KEYCODE_MENU:

                similar.setVisibility(View.VISIBLE);
                similar.bringToFront();
                similar.requestFocus();
                similar.bringToFront();
                nowHide()
}
public void nowHide(){
    new Thread(new Runnable() {
               public void run() {

                   while(true){
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {

                        e.printStackTrace();
                    } 

                      if(getLastInteractionTime()+5000 >= System.currentTimeMillis())
                      {
                          Log.d("MOVIE PLAY ACTIVITY:SADIP", "check time success");

                          runOnUiThread(new Runnable() {
                            public void run() {
                                similar.setVisibility(View.GONE);
                            }
                          });

                      }
                                            }
               }
                 }).start();
               }



 public long getLastInteractionTime() {
       return lastUsed;
    }

    public long setLastInteractionTime(int lastInteraction) {
       lastUsed = lastInteraction;
       return lastUsed;
    }

The code never reached to

if(getLastInteractionTime()+5000 >= System.currentTimeMillis())

And also I didn't got idea on stopping this thread

How can I do this? any other methods would be more appreciated Thanks in advance.


Solution

  • Try this. It will notify with a toast on user inactive after 5 seconds. onUserInteraction method is the main part of this code.

    Handler handler;
    Runnable r;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler = new Handler();
        r = new Runnable() {
    
            @Override
            public void run() {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "user inactive",
                        Toast.LENGTH_SHORT).show();
            }
        };
        startHandler();
    }
    @Override
    public void onUserInteraction() {
        // TODO Auto-generated method stub
        super.onUserInteraction();
        stopHandler();//stop first and then start
        startHandler();
    }
    public void stopHandler() {
        handler.removeCallbacks(r);
    }
    
    public void startHandler() {
        handler.postDelayed(r, 5000);
    }