Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中单击Listener,如何检查特定时间段内是否未单击任何按钮?_Android_Service_Onclick_Clicklistener - Fatal编程技术网

在Android中单击Listener,如何检查特定时间段内是否未单击任何按钮?

在Android中单击Listener,如何检查特定时间段内是否未单击任何按钮?,android,service,onclick,clicklistener,Android,Service,Onclick,Clicklistener,我的应用程序中有10个不同的按钮,用于执行不同的任务。我想开发一个持续检查(监听)的服务,如果用户在特定时间内没有点击任何按钮,比如说5秒,我希望执行其他任务。如何检查用户是否未单击任何按钮?如果有人有任何想法,请告诉我。在您的每个单击侦听器中,保存最后一个按钮被单击的时间: private long lastClickTimestamp; private Handler handler = new Handler(); public void onCreate( Bundle saved )

我的应用程序中有10个不同的按钮,用于执行不同的任务。我想开发一个持续检查(监听)的服务,如果用户在特定时间内没有点击任何按钮,比如说5秒,我希望执行其他任务。如何检查用户是否未单击任何按钮?如果有人有任何想法,请告诉我。

在您的每个单击侦听器中,保存最后一个按钮被单击的时间:

private long lastClickTimestamp;
private Handler handler = new Handler();

public void onCreate( Bundle saved ) {
   BackgroundJob job = new BackgroundJob();
   handler.postDelayed( job, SECONDS_TO_WAIT * 1000 );

   button1.setClickListener( new OnClickListener() {
       public void onClick( View view ) {
           lastClickTimestamp = System.currentTimeInMillis();
           // do the listener logic for button 1 here.
       }
   });

   button2.setClickListner( new OnClickListener() {
       public void onClick( View view ) {
           lastClickTimestamp = System.currentTimeInMillis();
           // do the listener logic for button 2 here.
       }
   });

   // repeat that for all 10 buttons.
}
现在,更聪明的开发人员将创建一个可重用的基类,处理一次时间戳的设置,然后在10个按钮中的每个按钮中重用该基类。但是,这取决于你。然后,后台作业将如下所示:

public class BackgroundJob implements Runnable {

   private boolean done = false;

  // meanwhile in job:
  public void run() {

     if( lastClickTimestamp > 0 && System.currentTimeInMillis() - lastClickTimestamp > SECONDS_TO_WAIT * 1000 ) {
       // let's do that job!
     }

     if( !done ) {
        // reschedule us to continue working
        handler.postDelayed( this, SECONDS_TO_WAIT * 1000 ); 
     }
  }
}

如果您必须使用某项服务,则可以向该服务发送通知,说明单击了某个按钮,然后该服务可以跟踪该按钮发生的时间。我不会为此使用服务,因为如果应用程序被放到后台或被杀死,播放动画或声音不需要存活。服务的用途是当某人在做其他事情时播放音乐、聊天应用程序或当用户不与应用程序交互时需要在后台运行的事情。您所描述的可以像我所展示的那样完成,因为当用户接到电话或短信时,他们会离开您的应用程序,您正在播放的动画或声音可能也应该停止。使用我展示的处理程序选项非常容易。使用服务更困难,但可行。

在每个单击侦听器中,节省最后一个按钮被单击的时间:

private long lastClickTimestamp;
private Handler handler = new Handler();

public void onCreate( Bundle saved ) {
   BackgroundJob job = new BackgroundJob();
   handler.postDelayed( job, SECONDS_TO_WAIT * 1000 );

   button1.setClickListener( new OnClickListener() {
       public void onClick( View view ) {
           lastClickTimestamp = System.currentTimeInMillis();
           // do the listener logic for button 1 here.
       }
   });

   button2.setClickListner( new OnClickListener() {
       public void onClick( View view ) {
           lastClickTimestamp = System.currentTimeInMillis();
           // do the listener logic for button 2 here.
       }
   });

   // repeat that for all 10 buttons.
}
现在,更聪明的开发人员将创建一个可重用的基类,处理一次时间戳的设置,然后在10个按钮中的每个按钮中重用该基类。但是,这取决于你。然后,后台作业将如下所示:

public class BackgroundJob implements Runnable {

   private boolean done = false;

  // meanwhile in job:
  public void run() {

     if( lastClickTimestamp > 0 && System.currentTimeInMillis() - lastClickTimestamp > SECONDS_TO_WAIT * 1000 ) {
       // let's do that job!
     }

     if( !done ) {
        // reschedule us to continue working
        handler.postDelayed( this, SECONDS_TO_WAIT * 1000 ); 
     }
  }
}

如果您必须使用某项服务,则可以向该服务发送通知,说明单击了某个按钮,然后该服务可以跟踪该按钮发生的时间。我不会为此使用服务,因为如果应用程序被放到后台或被杀死,播放动画或声音不需要存活。服务的用途是当某人在做其他事情时播放音乐、聊天应用程序或当用户不与应用程序交互时需要在后台运行的事情。您所描述的可以像我所展示的那样完成,因为当用户接到电话或短信时,他们会离开您的应用程序,您正在播放的动画或声音可能也应该停止。使用我展示的处理程序选项非常容易。使用服务更难,但可行。

在每个按钮上单击,将某些日历对象更新为新的日历实例,然后您可以检查该日历的时间,并查看是否超过5分钟

//this is a field
Calendar calendar;
public void onClick(View v) {
calendar = Calendar.getInstance();
//etc
switch(v.getId()) {
}
}

在每次单击按钮时,将某些日历对象更新为新的日历实例,然后您可以检查该日历的时间,并查看是否超过5分钟

//this is a field
Calendar calendar;
public void onClick(View v) {
calendar = Calendar.getInstance();
//etc
switch(v.getId()) {
}
}

您可以简单地将a设置为所需的长度。单击按钮时,只需重置计时器。在onResume中启动计时器,这样即使用户从电话或其他活动中回来,计时器也会启动。您可能也应该在活动暂停时停止计时器。

您可以简单地将a设置为所需的长度。单击按钮时,只需重置计时器。在onResume中启动计时器,这样即使用户从电话或其他活动中回来,计时器也会启动。您可能也应该在活动暂停时停止计时器。

感谢您的快速友好响应。我会试试这个,希望它对我有用。谢谢你快速而友好的回复。我会试试这个,希望它对我有用。谢谢你的快速回复。但我有10个不同的按钮。我希望如果在5秒钟内没有一个按钮被点击,那么我希望播放一些动画或声音。我希望使用android中的服务来实现这一点。那么你认为这是最好的方法吗。顺便说一句,再次感谢你的回答…我会修改我的答案,让它更清晰一点,但是你要为你的每个按钮重复注册码,因此如果单击了其中任何一个按钮,那么它们将设置LastClickedTime戳记。你可以把它重构成一个普通的类,你可以重复使用10次来减少重复,这就是我要做的。谢谢你的快速回复。但我有10个不同的按钮。我希望如果在5秒钟内没有一个按钮被点击,那么我希望播放一些动画或声音。我希望使用android中的服务来实现这一点。那么你认为这是最好的方法吗。顺便说一句,再次感谢你的回答…我会修改我的答案,让它更清晰一点,但是你要为你的每个按钮重复注册码,因此如果单击了其中任何一个按钮,那么它们将设置LastClickedTime戳记。你可以把它重构成一个普通的类,你可以重复使用10次,以减少重复,这就是我要做的。但是我如何检查用户在最后5-10秒内没有点击任何按钮呢?首先,我需要检查,如果用户并没有做任何活动的时间,然后只有我希望发挥一些声音。这就像用户在一段时间内处于非活动状态,然后播放一些声音通知……顺便说一句,感谢您的回答……但我如何检查用户在过去5-10秒内没有点击任何按钮?首先,我需要检查,如果用户并没有做任何活动的时间,然后只有我希望发挥一些声音。这就像如果用户在一段时间内处于非活动状态,然后播放一些声音通知…顺便说一句,感谢您的回答。。。