user1410081
user1410081

Reputation:

Bitmap wallpaper change every 10 seconds android

Hello I'm trying to create bitmap wallpaper. But this bitmap changes every 10 seconds. How can I accomplish this?

This is what I have tried:

 // I have declared 
 int[] images = {R.drawable.donna, R.drawable.donna1, R.drawable.marian,
        R.drawable.marian1, R.drawable.marian};


 Handler mHandler = new Handler();

 ImageView imgView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imgView =  (ImageView) findViewById(R.id.imageView1);

                new Thread(new Runnable() {
                    public void run() {
                        // TODO Auto-generated method stub
                        while (true) {
                            try {
                                Thread.sleep(10000);
                                mHandler.post(new Runnable() {

                                    public void run() {
                                        // TODO Auto-generated method stub
                                        // Write your code here to update the UI.
                                        Random ran = new Random();
                                        imgView.setImageResource(images[ran.nextInt(images.length)]);
                                    }
                                });
                            } catch (Exception e) {
                                // TODO: handle exception
                            }
                        }
                    }
                }).start(); 

But my question is how do I integrate the function/method for setting this as wallpaper?

Any help is truly appreciated. Thanks.

Upvotes: 0

Views: 1189

Answers (1)

Ali
Ali

Reputation: 2042

You can use postDelayed() to change your image wthin a specified timeframe:

Handler mHandler = new Handler(); 
Runnable __runnable = new Runnable()
{
    @Override
    public void run() 
    {
        Random ran = new Random();
        imgView.setImageResource(images[ran.nextInt(images.length)]);
        mHandler.postDelayed(this, 10000);              
    }
};
new Thread(__runnable).start();

For your second question, see the link below:

how to set image as wallpaper from the ImageViev

Upvotes: 1

Related Questions