A few days ago, I saw that the app for landscape wallpapers on the pea pod was very popular and I wanted to write a wallpaper program. As a beginner in Android development, a very important development step is to set permissions. I started to think that I had a problem with the wallpaper setup program. After reading the DOC file multiple times, I wrote several wallpaper settings. For your reference.
1. Do not forget to add the permission settings in applicationmanifest. xml.
<Uses-Permission Android: Name = "android. Permission. set_wallpaper"/>
2. Summary of how to set the wallpaper.
There are three wallpaper setting methods
First, use setbitmap () in the wallpapermanager method ()
Second, use setresource () in the wallpapermanager method ()
Third, use the setwallpaper () method provided in the contextwrapper class.
Because activity inherits contextthemewrapper, contextthemewrapper inherits contextwrapper.
1) Call the setbitmap () method in the singleton class by instantiating the wallpapermanager class.
Java code
- 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 successfully", Toast. length_short). Show ();
2) second, use setresource () in the wallpapermanager method ()
Java code
- Wallpapermanager = wallpapermanager. getinstance (this );
- Try {
- Wallpapermanager. setresource (getresources (). getidentifier ("Wallpaper" + imageposition, "drawable", "com. ch "));
- Toast. maketext (this, "set successfully", Toast. length_short). Show ();
- } Catch (ioexception e ){
- E. printstacktrace ();
- }
3) Third, use the setwallpaper () method Java code provided in the contextwrapper class
- // Override the setwallpaper () method in contextwrapper
- Public void setwallpaper (inputstream paraminputstream) throws ioexception {
- Super. setwallpaper (paraminputstream );
- Toast. maketext (this, "set successfully", 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 ();
- }
Conclusion: The wallpaper setting function is simple and can be used as our first app trainer. You can add controls such as imageswitcher and gallery. Truly complete a publishing app