2

I have an app in which the user can set the image view in any fragment as wallpaper using an action bar item. I am using the following code for 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)
              {
                  Toast.makeText(getActivity().getBaseContext(), "Wallpaper not set successfully",Toast.LENGTH_SHORT).show();
              }
        }
        }

        return false;   


    }

The problem I am facing is that when user clicks on the menu item, the app hangs for a few seconds before showing the toast and setting wallpaper. I am aware that we can use async task to solve this though i am not sure how exactly it can be used here. Please help me out regarding this. Thanks

EDIT: Here is my async task code:

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, "Wallpaer set successfully", Toast.LENGTH_SHORT).show();
    }
    protected Void doInBackground(Void... params) {
        WallpaperManager wm1=WallpaperManager.getInstance(c);
        try {
            wm1.setBitmap(ImageFrag1.bmg1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // TODO Auto-generated method stub
        return null;
    }

}
4

1 回答 1

1

据我了解,设置位图图像需要很长时间,这会迫使应用程序挂起。最好在任何并行线程而不是 ui 线程中执行此操作。您需要初始化一个 AsyncTask 并需要在 switch 语句中调用 execute() 方法。您需要在 AsyncTask 的 doInBackground 方法中执行该行。例如,试试这个..

private addImageTask mAddBitMap;

mAddBitMap.execute();

private class addImageTask extends AsyncTask<String, Void, Void> {

    protected void onPreExecute() {
    }

    @Override
    protected Void doInBackground(String... arg0) {
        addBitMap();
        return null;
    }

    protected void onPostExecute(Void unused) {

        try {
        } catch (Exception e) {
            Log.e(TAG, "Exception", e);
        }
    }
}
private void addBitMap(){
 WallpaperManager wm=WallpaperManager.getInstance(getActivity().getApplicationContext());
          try{
              wm.setBitmap(bmg1);
              Toast.makeText(getActivity().getBaseContext(), "Wallpaper set successfully",Toast.LENGTH_SHORT).show();
          }
          catch(IOException e)
          {
              Toast.makeText(getActivity().getBaseContext(), "Wallpaper not set successfully",Toast.LENGTH_SHORT).show();
          }
}
于 2013-07-31T17:53:40.163 回答