Hub*_*ert 284 multithreading android process
我想在两行代码之间暂停一下,让我解释一下:
- >用户单击一个按钮(实际上是一张卡片),我通过更改此按钮的背景来显示它:
thisbutton.setBackgroundResource(R.drawable.icon);
Run Code Online (Sandbox Code Playgroud)
- >让我们说1秒后,我需要通过改变它的背景来回到按钮的先前状态:
thisbutton.setBackgroundResource(R.drawable.defaultcard);
Run Code Online (Sandbox Code Playgroud)
- >我试图在这两行代码之间暂停线程:
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.也许这是我需要暂停的过程而不是线程?
我也尝试过(但它不起作用):
new Reminder(5);
Run Code Online (Sandbox Code Playgroud)
有了这个:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何暂停/休眠线程或进程?
tro*_*man 444
解决此问题的一种方法是使用Handler.postDelayed()方法.一些Google 培训材料提供了相同的解决方案.
@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);
}
Run Code Online (Sandbox Code Playgroud)
但是,有些人指出上述解决方案会导致内存泄漏,因为它使用非静态内部和匿名类,它隐式地保存对其外部类(活动)的引用.当活动上下文被垃圾收集时,这是一个问题.
一个更复杂的解决方案,避免内存泄漏,Handler并且Runnable在活动内部使用静态内部类,因为静态内部类不包含对其外部类的隐式引用:
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);
}
Run Code Online (Sandbox Code Playgroud)
请注意,对Activity Runnable使用WeakReference,这在需要访问UI的静态类中是必需的.
Daw*_*ozd 183
你可以尝试这个很短的
SystemClock.sleep(7000);
Run Code Online (Sandbox Code Playgroud)
警告:永远不要在UI线程上执行此操作.
用它来睡觉,例如.背景线程.
您的问题的完整解决方案将是:这是可用的API 1
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View button) {
button.setBackgroundResource(R.drawable.avatar_dead);
final long changeTime = 1000L;
button.postDelayed(new Runnable() {
@Override
public void run() {
button.setBackgroundResource(R.drawable.avatar_small);
}
}, changeTime);
}
});
Run Code Online (Sandbox Code Playgroud)
没有创建tmp Handler.此解决方案也优于@tronman,因为我们不会保留Handler的视图.我们也没有在坏线程中创建Handler的问题;)
public static void sleep(long ms)
在API级别1中添加
在返回之前等待给定的毫秒数(uptimeMillis).类似于sleep(long),但不会抛出InterruptedException ; interrupt()事件被推迟到下一个可中断操作.难道不返回直到至少指定的毫秒数已过时.
参数
返回前睡眠的ms,以毫秒为单位的正常运行时间.
从View类延迟的代码:
/**
* <p>Causes the Runnable to be added to the message queue, to be run
* after the specified amount of time elapses.
* The runnable will be run on the user interface thread.</p>
*
* @param action The Runnable that will be executed.
* @param delayMillis The delay (in milliseconds) until the Runnable
* will be executed.
*
* @return true if the Runnable was successfully placed in to the
* message queue. Returns false on failure, usually because the
* looper processing the message queue is exiting. Note that a
* result of true does not mean the Runnable will be processed --
* if the looper is quit before the delivery time of the message
* occurs then the message will be dropped.
*
* @see #post
* @see #removeCallbacks
*/
public boolean postDelayed(Runnable action, long delayMillis) {
final AttachInfo attachInfo = mAttachInfo;
if (attachInfo != null) {
return attachInfo.mHandler.postDelayed(action, delayMillis);
}
// Assume that post will succeed later
ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
return true;
}
Run Code Online (Sandbox Code Playgroud)
小智 25
我用这个:
Thread closeActivity = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
// Do some stuff
} catch (Exception e) {
e.getLocalizedMessage();
}
}
});
Run Code Online (Sandbox Code Playgroud)
小智 14
我使用CountDownTime
new CountDownTimer(5000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// do something after 1s
}
@Override
public void onFinish() {
// do something end times 5s
}
}.start();
Run Code Online (Sandbox Code Playgroud)
这就是我在一天结束时所做的 - 现在工作正常:
@Override
public void onClick(View v) {
my_button.setBackgroundResource(R.drawable.icon);
// SLEEP 2 SECONDS HERE ...
final Handler handler = new Handler();
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
my_button.setBackgroundResource(R.drawable.defaultcard);
}
});
}
}, 2000);
}
Run Code Online (Sandbox Code Playgroud)
除了Yankowsky先生的答案,你也可以使用postDelayed().这可以在任何View(例如,您的卡)上使用,并且需要Runnable一段延迟时间.它执行Runnable延迟之后.
小智 5
这是我的例子
创建 Java 实用程序
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
public class Utils {
public static void showDummyWaitingDialog(final Context context, final Intent startingIntent) {
// ...
final ProgressDialog progressDialog = ProgressDialog.show(context, "Please wait...", "Loading data ...", true);
new Thread() {
public void run() {
try{
// Do some work here
sleep(5000);
} catch (Exception e) {
}
// start next intent
new Thread() {
public void run() {
// Dismiss the Dialog
progressDialog.dismiss();
// start selected activity
if ( startingIntent != null) context.startActivity(startingIntent);
}
}.start();
}
}.start();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
或者你可以使用:
android.os.SystemClock.sleep(checkEvery)
Run Code Online (Sandbox Code Playgroud)
它的优点是不需要包装try ... catch。
| 归档时间: |
|
| 查看次数: |
584181 次 |
| 最近记录: |