Android 线程的等待活动
我如何等待活动中的线程处理。我的线程从远程服务器下载 .png 图片。我想等待下载图片,然后活动将继续处理。
活动
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
}
此代码在没有下载图片的情况下继续,图片稍后下载。信号量适合这个目的吗?
How can I wait for thread processing in the activity. My thread downloads .png picture from remote server. I want to wait for downloading the picture, and then the activity will be continue in the processing.
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
}
This code continues without downloaded picture, the picture is downloaded later. Are the semaphores suitable for this purpose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不想受限于 ASyncTask,请使用 BroadcastReceiver。您将通过在活动的 OnResume() 方法中注册来监听主类中的 BroadcastReceiver。当您的服务或后台活动完成时,您将触发 BroadcastReceiver 告诉活动后台服务已完成。
If you don't want to be constrained to an ASyncTask, use a BroadcastReceiver. You will listen for the BroadcastReceiver in your main class by registering it in the OnResume() method of the activity. When your Service or background activity finishes you will trigger the BroadcastReceiver to tell the activity the background service is done.
使用 AsyncTask 与 LoadingBar 对话框配对。这将显示 png 下载的进度。
下载完成后,使用 AsyncTask 中的 onPostExecute() 方法继续执行您希望 Activity 执行的操作。
Use an AsyncTask paired with a LoadingBar Dialog. This will display progress as the png downloads.
Then once the download is complete use the onPostExecute() method in AsyncTask to continue on with what ever you want your activity to do.
为此,您应该使用 AsyncTask。
这是一个例子
}
You should use AsyncTask for this purpose.
Here is an example
}