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() {

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.