如何在 Activity 中等待线程处理。我的线程从远程服务器下载 .png 图片。我想等待下载图片,然后 Activity 将在处理中继续。
Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.initializer);
ImageView mainImageView = (ImageView) findViewById(R.id.imageView);
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
InitProgress initProgress = new InitProgress(progressBar); // initialization progress bar
MyMap map = new MyMap(); // there is written the picture as Drawable from thread
MapDownloader mapDownloader = new MapDownloader(map,initProgress); // thread that download picture from server
// wait ???
mainImageView.setBackgroundDrawable(buildingMap.getDrawableMap()); // using downloaded picture
}
这段代码继续没有下载图片,图片是稍后下载的。信号量适合这个目的吗?
最佳答案
如果您不想受限于 ASyncTask,请使用 BroadcastReceiver。您将通过在 Activity 的 OnResume() 方法中注册来监听主类中的 BroadcastReceiver。当您的服务或后台 Activity 完成时,您将触发 BroadcastReceiver 以告知 Activity 后台服务已完成。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, WordService.class);
context.startService(service);
}
关于android等待线程的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8764067/