onClick事件中含有异步执行的方法,如何让异步方法执行完毕后onClick事件方可重新响应点击事件

最近在做批量关闭应用程序的一个应用,双击home键,显示所有在后台运行的应用程序的图标,点击应用程序图标来移除图标,后台应用图标移动后重新排列,好了,问题来了,当我快速点击图标时,前一个图标还没来得及移除,后面的一个图标就已经移动到了该图标所在的位置,从而导致两个三个甚至更多图标重叠到一起,有时还会导致手机关机重启

想了各种办法:最后的解决策略是

private boolean unlocked = true;
 public void onClick(View sview) {

     if(!unlocked) return;
     unlocked = false;

        final View view = sview;
   
        IphoneTaskbar.this.postDelayed(new Runnable(){
   public void run() {  

  Object tag = view.getTag();
  if(tag instanceof ItemInfo){
   final Intent intent = ((ItemInfo) tag).intent;
   if(mWorkspace.mShaking){

    IphoneTaskbarCellLayout cellLayout = (IphoneTaskbarCellLayout)mWorkspace.getChildAt(mWorkspace.getCurrentScreenMusic());
                if(cellLayout == null){
     return ;
    }    
    if(true/*cellLayout.mLeaveX < 30 && cellLayout.mLeaveY < 30*/){
              final ActivityManager am = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
              String str = intent.getPackage();

             if(str.equals("com.duomi.android") || str.equals("cn.kuwo.player")){
      try{
      Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class); 
      forceStopPackage.setAccessible(true); 
      forceStopPackage.invoke(am, str);
      }
      catch(InvocationTargetException e){
       Log.d(TAG,"there is a InvocationTargetException");
      }
      catch(NoSuchMethodException e){
       Log.d(TAG,"there is a NoSuchMethodException");
      }
      catch(IllegalAccessException e){
       Log.d(TAG,"there is a IllegalAccessException");
      }
     }  
     
       else if(str.equals("com.mediatek.FMRadio")){    
                                       try{
                 Log.d("TAG","str==com.mediatek.FMRadio");         
      Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class); 
      forceStopPackage.setAccessible(true); 
      forceStopPackage.invoke(am, str);
      }
      catch(InvocationTargetException e){
       Log.d(TAG,"there is a InvocationTargetException");
      }
      catch(NoSuchMethodException e){
       Log.d(TAG,"there is a NoSuchMethodException");
      }
      catch(IllegalAccessException e){
       Log.d(TAG,"there is a IllegalAccessException");
      }
     }else{
      am.killBackgroundProcesses(str);
     }
       
           final int cellX = ((ItemInfo) tag).cellX;
           final int childCount = cellLayout.getChildCount();
           cellLayout.removeView(view);
           if(childCount < 4){
            for(int i = cellX + 1; i < childCount; i++){
             View child = cellLayout.getChildAtIndex(i);
     
      if(child != null){
             ItemInfo info = (ItemInfo) child.getTag();
             info.cellX = i - 1;
             cellLayout.onAnimateShiftChild(child, (i -1));
      }
      
            }
           }else{
            for(int i = cellX + 1; i <= childCount; i++){
             if(i == 4){
              IphoneTaskbarCellLayout nextCellLayout = (IphoneTaskbarCellLayout)mWorkspace.getChildAt(mWorkspace.getCurrentScreenMusic() + 1);
              if(nextCellLayout != null){
               View child = nextCellLayout.getChildAtIndex(0);
               ItemInfo info = (ItemInfo) child.getTag();
               info.cellX = 3;
               info.screen = mWorkspace.getCurrentScreenMusic();
               nextCellLayout.removeView(child);
               mWorkspace.addInScreen(0,child, info.screen, info.cellX);
               cellLayout.onAnimateShiftChildFromNextGroup(child);
               
               IphoneTaskbarCellLayout targetCellLayout;
               View childView;
               ItemInfo childInfo;
               for(int j = mWorkspace.getCurrentScreenMusic() + 1; j < mWorkspace.getChildCount(); j++){
                targetCellLayout = (IphoneTaskbarCellLayout)mWorkspace.getChildAt(j);
                if(targetCellLayout == null){
                 break;
                }
                if(targetCellLayout.getChildCount() == 0){
                 mWorkspace.removeView(targetCellLayout);
                 break;
                }else{
                 for(int k = 0; k < 4; k++){
                  if(k == 3){
                   IphoneTaskbarCellLayout targetNextCellLayout = (IphoneTaskbarCellLayout)mWorkspace.getChildAt(j + 1);
                   if(targetNextCellLayout != null){
                    childView = targetNextCellLayout.getChildAtIndex(0);
                    childInfo = (ItemInfo) childView.getTag();
                    childInfo.cellX = 3;
                    childInfo.screen = j;
                    targetNextCellLayout.removeView(childView);
                    mWorkspace.addInScreen(0,childView, childInfo.screen, childInfo.cellX);
                    targetCellLayout.onShiftChild(childView, childInfo.cellX);
                   }
                  }else{
                   if(k == targetCellLayout.getChildCount()){
                    break;
                   }
                   childView = targetCellLayout.getChildAtIndex(k + 1);
         
            if(childView != null){
                       childInfo = (ItemInfo) childView.getTag();
                       childInfo.cellX = k;
                       targetCellLayout.onShiftChild(childView, childInfo.cellX);
            }
         
                  }
                 }
                 targetCellLayout.invalidate();
                }
               }
              }
             }else{
              View child = cellLayout.getChildAtIndex(i);
     
       if(child!=null){
              ItemInfo info = (ItemInfo) child.getTag();
              info.cellX = i - 1;
              cellLayout.onAnimateShiftChild(child, info.cellX);
       }
   
             }
            }
           }
           //mWorkspace.invalidate();
    }
 
   }else{
      try{
             int[] pos = new int[2];
             view.getLocationOnScreen(pos);
             intent.setSourceBounds(new Rect(pos[0], pos[1],
                     pos[0] + view.getWidth(), pos[1] + view.getHeight()));
             startActivitySafely(intent, tag);
             mCallbacks.onCancel();
        }catch(Exception e){
                    
       }
    
   }
  }else if(view.getId() == R.id.workspace_container){
   onEndAnimation();
  }
  unlocked = true;
   }
  }, ANIMATION_DURATION);
  
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值