Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 539
A_rmas Avatar asked Feb 12 '23 00:02

A_rmas


1 Answers

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);
}
like image 91
Illegal Argument Avatar answered Feb 15 '23 10:02

Illegal Argument