Calculate time thread android

I’m using a thread to set an image as background and in this thread i have a dialog. The dialog starts and should be close when the wallpaper will be set. This is the code so far

setWallbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final ProgressDialog myPd_ring=ProgressDialog.show(SingleWall.this, "Setting wallpaper", "", true);
                new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                           // TODO Auto-generated method stub
                            WallpaperManager wallManager = WallpaperManager.getInstance(getApplicationContext());
                            try {
                                image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                                wallManager.setBitmap(image);
                                Toast.makeText(SingleWall.this, "Wallpaper Set Successfully!!", Toast.LENGTH_SHORT).show();
                                myPd_ring.dismiss();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                Toast.makeText(SingleWall.this, "Setting WallPaper Failed!!", Toast.LENGTH_SHORT).show();
                                myPd_ring.dismiss();
                            }
                        }
                    }, 4000);


            }
        });

So, on click in a button starts the thread and for 4 seconds the dialog should be visible with the progress icon. But it is not correct! the time to set the background could be more or less than 4 seconds! So the 4000 should be calculates in base of the time to set the image as wallpaper. Is it possible?

ps. I can’t use a AsyncTask because i get many NullPointerExceptions


Source: android

Leave a Reply