Android Wallpaper Setup Code

Source: Internet
Author: User

Make a list of game pictures, think of adding the wallpaper function, the difference is some information.

1 Don't forget to add permission settings to the Applicationmanifest.xml.

<uses-permission android:name = "Android.permission.SET_WALLPAPER"/>

2, set the method summary of the wallpaper.

There are three ways to set the wallpaper

First through the Wallpapermanager method of the SetBitmap ()

The second through the Wallpapermanager method of the Setresource ()

Third through the SetWallpaper () method provided in the Contextwrapper class

Because Activity inherits Contextthemewrapper, Contextthemewrapper inherits Contextwrapper.

1) invoke the SetBitmap () method in the Singleton class by instantiating the Wallpapermanager class.

Wallpapermanager Wallpapermanager = Wallpapermanager.getinstance (this);
Resources res = getresources ();
Bitmap Bitmap=bitmapfactory.decoderesource (res, getresources (). Getidentifier ("wallpaper" + imageposition, "drawable "," com.ch "));
Wallpapermanager.setbitmap (bitmap);
Toast.maketext (This, "set success", Toast.length_short). Show ();

2) Second through the Wallpapermanager method of the Setresource ()

wallpapermanager Wallpapermanager = Wallpapermanager.getinstance (this);
try {
Wallpapermanager.setresource (Getresources (). Getidentifier ("wallpaper" + imageposition, "drawable", "com.ch"));
Toast.maketext (This, "set success", Toast.length_short). Show ();
} catch (IOException e) {
E.printstacktrace ();
}

3) third through the SetWallpaper () method provided in the Contextwrapper class

Overriding the SetWallpaper () method in Contextwrapper
public void SetWallpaper (InputStream paraminputstream) throws IOException {
Super.setwallpaper (Paraminputstream);
Toast.maketext (This, "set success", 1). Show ();
}
Set the wallpaper code
Resources localresources = Getbasecontext (). Getresources ();
InputStream localInputStream2 = localresources
. Openrawresource (Getresources (). Getidentifier (
"Wallpaper" + imageposition, "drawable", "com.ch"));
try {
SetWallpaper (LOCALINPUTSTREAM2);
} catch (IOException e) {
E.printstacktrace ();
}

For these three kinds, the feeling or the second chapter is the best use, can transform the network picture to the Bitmap type, then calls the system Wallpapermanager to make the corresponding method call.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.