2016-06-08 155 views
-1

我有一個Android應用程序發送喜歡到服務器。2秒後如何檢查按鈕是否被點擊?

我想要做的不是立即向服務器發送消息,而是在2秒後發送,如果用戶仍喜歡該消息。

我喜歡空白;

public void rotationAnimation(ImageView button, int source1, int source2){ 
    if(isLikeClicked){ 
     button.setImageResource(source1); 
     button.startAnimation(rotate_backward); 
     isLikeClicked = false; 
    }else{ 
     button.setImageResource(source2); 
     button.startAnimation(rotate_forward); 
     isLikeClicked = true; 
    } 

    ChangeLikeCount(); 

    if(isReadyToPost) 
     if(!isLikeClicked){ 
      Like like = new Like(); 
      like.execute(ServerCons.HOST + "unlike"); 
     }else{ 
      Like like = new Like(); 
      like.execute(ServerCons.HOST + "like"); 
     } 

    new Handler().postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      // I thought the solution could be there 
     } 
    }, 2000); 
} 

回答

0

試試這種方法:

boolean isReadyToPost= false; 
boolean liked = false;//control like click (witch) 

public void onLikePressed() { 
    if (liked && isReadyToPost) { 
    sendLikeToServer();//send to server after 2 secs 
    return; 
    } 

    this.isReadyToPost= false; 
    Toast.makeText(this, "waiting for any dislike... in 2 secs", Toast.LENGTH_SHORT).show(); 
if (liked){ 
    new Handler().postDelayed(new Runnable() { 

    @Override 
    public void run() { 
     isReadyToPost=true; 
     onLikePressed();      
    } 
    }, 2000); 
}//end if 
} //end onlikepress 
0

你可以嘗試創建休眠2秒之後,它會檢查,如果用戶仍然喜歡那麼後更新數據庫

1

不要使用isReadyToPost它標誌一個線程:

if(isReadyToPost){ 
    isReadyToPost=false; 
}else{ 
    // try after 2 secs for next like 
} 

Handler. postDelayed變化isReadyToPost爲真後2秒:

@Override 
    public void run() { 
     isReadyToPost=true; 
    } 

isReadyToPost默認值爲true

相關問題