我正在使用异步任务在菜单项单击时设置墙纸,但它会继续运行并且不会停止。这是我正在使用的代码:
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;
}
}
在项目选择功能中,我正在这样做:
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;
}
我哪里错了?谢谢