如何在 Android 中暂停/休眠线程或进程? [英] How to pause / sleep thread or process in Android?

查看:79
本文介绍了如何在 Android 中暂停/休眠线程或进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两行代码之间暂停一下,让我解释一下:

I want to make a pause between two lines of code, Let me explain a bit:

-> 用户点击一个按钮(实际上是一张卡片),我通过改变这个按钮的背景来显示它:

-> the user clicks a button (a card in fact) and I show it by changing the background of this button:

thisbutton.setBackgroundResource(R.drawable.icon);

-> 假设 1 秒后,我需要通过更改背景来返回按钮的先前状态:

-> after let's say 1 second, I need to go back to the previous state of the button by changing back its background:

thisbutton.setBackgroundResource(R.drawable.defaultcard);

-> 我试图暂停这两行代码之间的线程:

-> I've tried to pause the thread between these two lines of code with:

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

然而,这行不通.也许我需要暂停的是进程而不是线程?

However, this does not work. Maybe it's the process and not the Thread that I need to pause?

我也试过(但没用):

new Reminder(5);

有了这个:

public class Reminder {

Timer timer;

        public Reminder(int seconds) {
            timer = new Timer();
            timer.schedule(new RemindTask(), seconds*1000);
        }

        class RemindTask extends TimerTask {
            public void run() {
                System.out.format("Time's up!%n");
                timer.cancel(); //Terminate the timer thread
            }
        }  
    }

如何暂停/休眠线程或进程?

How can I pause/sleep the thread or process?

推荐答案

这个问题的一个解决方案是使用 Handler.postDelayed() 方法.一些 Google 培训材料提出了相同的解决方案.

One solution to this problem is to use the Handler.postDelayed() method. Some Google training materials suggest the same solution.

@Override
public void onClick(View v) {
    my_button.setBackgroundResource(R.drawable.icon);

    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() {
         @Override 
         public void run() { 
              my_button.setBackgroundResource(R.drawable.defaultcard); 
         } 
    }, 2000); 
}

然而,有些人指出上面的解决方案导致内存泄漏,因为它使用了一个非静态的内部匿名类,该类隐式地持有对其外部类活动的引用.当活动上下文被垃圾收集时,这是一个问题.

However, some have pointed out that the solution above causes a memory leak because it uses a non-static inner and anonymous class which implicitly holds a reference to its outer class, the activity. This is a problem when the activity context is garbage collected.

避免内存泄漏的更复杂的解决方案将 HandlerRunnable 子类化为活动内部的静态内部类,因为静态内部类不持有对其的隐式引用外部类:

A more complex solution that avoids the memory leak subclasses the Handler and Runnable with static inner classes inside the activity since static inner classes do not hold an implicit reference to their outer class:

private static class MyHandler extends Handler {}
private final MyHandler mHandler = new MyHandler();

public static class MyRunnable implements Runnable {
    private final WeakReference<Activity> mActivity;

    public MyRunnable(Activity activity) {
        mActivity = new WeakReference<>(activity);
    }

    @Override
    public void run() {
        Activity activity = mActivity.get();
        if (activity != null) {
            Button btn = (Button) activity.findViewById(R.id.button);
            btn.setBackgroundResource(R.drawable.defaultcard);
        }
    }
}

private MyRunnable mRunnable = new MyRunnable(this);

public void onClick(View view) {
    my_button.setBackgroundResource(R.drawable.icon);

    // Execute the Runnable in 2 seconds
    mHandler.postDelayed(mRunnable, 2000);
}

请注意,Runnable 使用 WeakReference 到 Activity,这在需要访问 UI 的静态类中是必需的.

Note that the Runnable uses a WeakReference to the Activity, which is necessary in a static class that needs access to the UI.

这篇关于如何在 Android 中暂停/休眠线程或进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆