按下键后android检测用户不活动

按下键后android检测用户不活动

本文介绍了按下键后android检测用户不活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的媒体播放器中,我添加了一个视图,如果按下菜单键会显示该视图,我希望它在一段时间后被隐藏,即用户不想看到该视图,该视图是水平的,我希望它几秒钟后隐藏起来,用户将不会按向右或向左键.

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

代码从未达到

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

我也不知道停止该线程

我该怎么做?任何其他方法将不胜感激预先感谢.

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

推荐答案

尝试一下. 5秒钟后,它将在用户不活动的情况下发出敬酒通知. onUserInteraction 方法是此代码的主要部分.

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

这篇关于按下键后android检测用户不活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 16:20