John
John

Reputation: 25

Android: alert dialog in a class without activity

i have this class:

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

ImageFromWeb ifw;
private String url;
private final WeakReference<ImageView> imageViewReference;

public DownloadImageTask(ImageView imageView) {
    imageViewReference = new WeakReference<ImageView>(imageView);
}

@Override
protected Bitmap doInBackground(String... params) {
    url = params[0];
    try {
        return BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

@Override
protected void onPostExecute(Bitmap result) {
    if (isCancelled()) {
        result = null;
    }
    if (imageViewReference != null) {
        ImageView imageView = imageViewReference.get();
        if (imageView != null) {
            imageView.setImageBitmap(result);
        }
    }
}

@Override
protected void onPreExecute() {
    if (imageViewReference != null) {
        ImageView imageView = imageViewReference.get();
        if (imageView != null) {
 ---------> imageView.setImageResource(R.drawable.pw);     
        }
    }
}

}

and the main activity:

public class ImageFromWeb extends Activity {

private String path = "http://....";
private ImageView imageView;

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.main);

    ImageView mChart = (ImageView) findViewById(R.id.imview);

    mChart.setTag(path);
    new DownloadImageTask(mChart).execute(path);
}

}

I want to put in the point of arrow(in DownloadImageTask class) an alert dialog! How can i do this? Because this class isn't an activity.

thanks :)

Upvotes: 1

Views: 3082

Answers (4)

Jmorvan
Jmorvan

Reputation: 1040

You can have a static Context in your Application like this:

public static Context CurrentContext;

and a custom abstract Activity that sets currentContext upon creation like this:

public abstract class CustomActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.CurrentContext = this;
    }
}

Then you would get context like this:

AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(MyApplication.CurrentContext);
                    dlgBuilder.setTitle("Context Example");
                    dlgBuilder.setMessage("I am being shown from the application Static context!");
                    dlgBuilder.setNeutralButton("Ok", null);
                    dlgBuilder.show();

This way you never have to worry about context wether you are in a background task or directly in an Activity it will work for most cases.

hope this helps!

Upvotes: 0

ngesh
ngesh

Reputation: 13501

pass a Activity instance to the class where you want to display dialog, and check

if(!actvity.isFinishing){
//show dialog
}

Upvotes: 0

Steve Bergamini
Steve Bergamini

Reputation: 14600

Move the Async Task to your activity and use that to call your DownloadImageTask class & methods. This will make your life a lot easier.

Upvotes: 0

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

change the constructor and pass a Context object

Context mContext;
public DownloadImageTask(ImageView imageView,Context mContext) {
    imageViewReference = new WeakReference<ImageView>(imageView);
    this.mContext = mContext;
}

Now you can use this Context to create dialogs

You can even cast mContext to your Activity class and call functions within your Activity

Upvotes: 4

Related Questions