Android 线程的等待活动

发布于 2024-12-25 08:38:53 字数 865 浏览 2 评论 0原文

我如何等待活动中的线程处理。我的线程从远程服务器下载 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人间☆小暴躁 2025-01-01 08:38:53

如果您不想受限于 ASyncTask,请使用 BroadcastReceiver。您将通过在活动的 OnResume() 方法中注册来监听主类中的 BroadcastReceiver。当您的服务或后台活动完成时,您将触发 BroadcastReceiver 告诉活动后台服务已完成。

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);
        }

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.

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);
        }
萌化 2025-01-01 08:38:53

使用 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.

乖乖 2025-01-01 08:38:53

为此,您应该使用 AsyncTask。

这是一个例子

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

ImageView imageView = null;

@Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}

@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}


private Bitmap download_Image(String url) {
...

}

You should use AsyncTask for this purpose.

Here is an example

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

ImageView imageView = null;

@Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}

@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}


private Bitmap download_Image(String url) {
...

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文