使用 Wallpaper Manager 的 Android AsyncTask

标签 android asynchronous android-asynctask imageview wallpaper

我的墙纸管理器使用异步任务设置为墙纸。但是从非异步更改为 AsyncTask 后,我收到错误消息“方法 getBaseContext() 未定义 SetWallpaperTask 类型”请更正我的代码。非常感谢。

旧的非异步任务

public void SetWallpaper(String image_url)
        {   
        URL myFileUrl = null;
            try
        {   
                myFileUrl = new URL(image_url); 
        }
            catch (MalformedURLException e)
            {      
                e.printStackTrace();  
            }   
            Bitmap bmImg = null;
            try {  
                HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();   
                conn.setDoInput(true);   
                conn.connect();     
                //int length = conn.getContentLength(); 
            InputStream is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is); 
            }
                catch (IOException e)
                {       
                    e.printStackTrace();  
                }   
        try {       

            String path = myFileUrl.getPath();
            String idStr = path.substring(path.lastIndexOf('/') + 1);
            File filepath = Environment.getExternalStorageDirectory();
            File dir = new File (filepath.getAbsolutePath() + "/Wallpaper/");
                dir.mkdirs();
                String fileName = idStr;
                File file = new File(dir, fileName);
                FileOutputStream fos = new FileOutputStream(file);

                bmImg.compress(CompressFormat.JPEG, 75, fos);   
                fos.flush();    
                fos.close();       

                WallpaperManager wpm = WallpaperManager.getInstance(getBaseContext());
                wpm.setBitmap(bmImg);

        }
        catch (Exception e){


            e.printStackTrace();  
        }
        }

新建异步任务

public class SetWallpaperTask extends AsyncTask<String , String , String>
{
    private Context context;
    private ProgressDialog pDialog;
    String image_url;
    URL myFileUrl;
    String myFileUrl1;
    Bitmap bmImg = null;

    public SetWallpaperTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub

        super.onPreExecute();

        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Downloading Image ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub

        try {  

            myFileUrl = new URL(args[0]);
            //myFileUrl1 = args[0];

            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();   
            conn.setDoInput(true);   
            conn.connect();     
            InputStream is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is); 
        }
        catch (IOException e)
        {       
            e.printStackTrace();  
        }
        try {       

            String path = myFileUrl.getPath();
            String idStr = path.substring(path.lastIndexOf('/') + 1);
        File filepath = Environment.getExternalStorageDirectory();
        File dir = new File (filepath.getAbsolutePath() + "/Wallpaper/");
            dir.mkdirs();
            String fileName = idStr;
            File file = new File(dir, fileName);
            FileOutputStream fos = new FileOutputStream(file);
            bmImg.compress(CompressFormat.JPEG, 75, fos);   
            fos.flush();    
            fos.close();    

        }
        catch (Exception e)
                {
                    e.printStackTrace();  
                }
        return null;   
    }


    @Override
    protected void onPostExecute(String args) {
        // TODO Auto-generated method stub
        WallpaperManager wpm = WallpaperManager.getInstance(context()); // --The method context() is undefined for the type SetWallpaperTask
        wpm.setBitmap(bmImg);
        pDialog.dismiss();

    }


}

最佳答案

这是因为您无法使用 getBaseContext() 在 AsyncTask 中获取上下文。

我看到您已经在构造函数中接收了一个 Context 并将其存储在类变量 context 中。所以你可以简单地改变

    WallpaperManager wpm = WallpaperManager.getInstance(getBaseContext()); 

    WallpaperManager wpm = WallpaperManager.getInstance(context);

关于使用 Wallpaper Manager 的 Android AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12016445/

相关文章:

java - Android重建项目,包org.junit不存在错误

silverlight - Socket.SendAsync 线程安全有效吗?

javascript - Node.js 异步 : How to combine multiple iterators with map?

android - 使用来自 url 的图像折叠工具栏?

android - 使用 Eclipse 3.6.1 正确安装 Android SDK、ADT

android - 如何在不增加 min sdk 的情况下更改操作栏的背景颜色?

node.js - 围绕 async wait 编写一个循环来并行读取和写入文件?

android - JSoup 解析元素为空

android - 如何创建 asyncTask 以防止 networkOnMainThreadException

android - 如果在底部,ExpandableListView 不会滚动