Shivam Bhalla
Shivam Bhalla

Reputation: 1909

Android async task running continuously and not stopping

I am using an async task to set wallpaper on menu item click but it keeps on running and does not stop. Here is the code I am using:

class ImageTask extends AsyncTask<Void, Void, Void> {
    Context c;
    ProgressDialog pd;

    public ImageTask(Context ctx) { this.c=ctx; }

    @Override
    protected void onPreExecute() {
        pd=ProgressDialog.show(c, "Please Wait", "Setting Wallpaper...");
    }

    public void onPostExecute() {
        pd.dismiss();
        Toast.makeText(c, "Wallpaper set successfully", Toast.LENGTH_SHORT).show();
    }

    protected Void doInBackground(Void... params) {
        WallpaperManager wm1=WallpaperManager.getInstance(c);

        try { wm1.setBitmap(ImageFrag1.bmg1); } 
        catch (IOException e) { e.printStackTrace(); }

        return null;
    }
}

and in the item selected function I am doing this:

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_wall: {
            /*WallpaperManager wm=WallpaperManager.getInstance(getActivity().getApplicationContext());
            try{
                wm.setBitmap(bmg1);
                Toast.makeText(getActivity().getBaseContext(), "Wallpaper set successfully",Toast.LENGTH_SHORT).show();
            } catch(IOException e) {
                oast.makeText(getActivity().getBaseContext(), "Wallpaper not set successfully",Toast.LENGTH_SHORT).show();
            }*/
            ImageTask it1=new ImageTask(getActivity());
            it1.execute();
        }
    }
    return false;   
}

Where am i going wrong? Thanks

Upvotes: 1

Views: 1166

Answers (1)

laalto
laalto

Reputation: 152797

The onPostExecute() method signature is not correct and does not override a function in the AsyncTask superclass. The async task is not "running continuously", it's just that the progress dialog never gets dismissed.

To fix it, replace

 public void onPostExecute()

with

 @Override
 protected void onPostExecute(Void result)

Upvotes: 7

Related Questions