当前位置: 首页 > 知识库问答 >
问题:

Android应用程序与调试器一起工作,但不是在发布模式下

沈巴英
2023-03-14

我一直在尝试为我的应用程序实现一个去噪器,我试图使用这个去噪器减少对服务器的请求,最终,我设法实现了去噪器,但是,它似乎只有在我使用调试器调试应用程序时才起作用。

这就是我实现调试器的方式

public class NewDebouncedRunnable implements Runnable {

    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    private final Runnable operation;
    private final long delayMillis;
    private ScheduledFuture<?> scheduledFuture;

    private long lastRunTime = -1;
    private boolean isQueued = false;
    private Context context;

    public NewDebouncedRunnable(Context context,Runnable operation, String name, long delayMillis) {
        this.operation = operation;
        this.delayMillis = delayMillis;
        this.context = context;
    }

    public void synchronized run() {

        if(lastRunTime==-1){
            Toasty.success(context,"LastRunTime reset",0).show();
        }

        long currentTime = getCurrentTimeMillis();

        System.out.println(currentTime-lastRunTime);

        if (shouldRunNow(currentTime)) {
            // we've never called this before, call it now
            System.out.println("Registered");
            lastRunTime = currentTime;
            operation.run();
        } else {
            System.out.println("Queued");
            if(scheduledFuture!=null){
                scheduledFuture.cancel(true);
            }
            schedule(this::scheduledRun, delayMillis);
        }
    }

    private void scheduledRun() {
        Log.d("SCHEDULED RUN","running scheduled task");
        lastRunTime = getCurrentTimeMillis();
        isQueued = false;
        operation.run();
    }

    private boolean shouldRunNow(long currentTime) {
        return currentTime-lastRunTime > delayMillis;
    }

   
    private void schedule(Runnable call, long delayMillis) {
        scheduledFuture = scheduler.schedule(call, delayMillis, TimeUnit.MILLISECONDS);
    }

  
    long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }
}

这是我的可定义运行实例

 NewDebouncedRunnable increaseOrderItemDebounce = new NewDebouncedRunnable(context,new Runnable() {
            @Override
            public void run() {
                try{
                    Log.d("DEBOUNCE","Debounced run");
                    
                    HelperClass.getInstance().updateOrderItem(context, item, item.getQuantity(), new HelperClass.ResponseListener<ResponseAddUpdateToCart>() {
                        @Override
                        public void onSuccess(ResponseAddUpdateToCart response) {
                            Toasty.success(context,"Cart Updated", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(String error) {

                        }
                    });

                 
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        },"",20000);

这是我为按钮设置onClickListener的方式

 holder.btnPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                item.setQuantity(item.getQuantity()+1);
                increaseOrderItemDebounce.run();
            }
        });

我还应该注意,该按钮是recycler视图的一部分,在向服务器发出成功请求后,适配器会收到通知,使用adapter更改了项目。notifyItemRangeChanged()如下所示

if (orderListAdapter != null && orderlistRview.getLayoutManager() != null) {
                Parcelable recyclerViewState = orderlistRview.getLayoutManager().onSaveInstanceState();
      
                orderlistRview.getLayoutManager().onRestoreInstanceState(recyclerViewState);
         
                orderListAdapter.notifyItemRangeChanged(0,orderListAdapter.getItemCount());

            }

我在这里面临的主要问题是,每次我在常规释放模式下单击按钮时,lastRunTime变量都会重置,导致对服务器的连续请求,但当我使用调试器运行应用程序并设置断点时,它会按预期工作。

共有1个答案

唐哲
2023-03-14

在格拉德尔试试这个。建筑

建筑类型{

    release {

        **debuggable true** 

        minifyEnabled false

        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

    }

}
 类似资料: