Android wallpaper settings

Source: Internet
Author: User

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
  1. Wallpapermanager = wallpapermanager. getinstance (this );
  2. Resources res = getresources ();
  3. Bitmap bitmap = bitmapfactory. decoderesource (Res, getresources (). getidentifier ("Wallpaper" + imageposition, "drawable", "com. ch "));
  4. Wallpapermanager. setbitmap (Bitmap );
  5. Toast. maketext (this, "set successfully", Toast. length_short). Show ();

2) second, use setresource () in the wallpapermanager method ()

 

Java code
  1. Wallpapermanager = wallpapermanager. getinstance (this );
  2. Try {
  3. Wallpapermanager. setresource (getresources (). getidentifier ("Wallpaper" + imageposition, "drawable", "com. ch "));
  4. Toast. maketext (this, "set successfully", Toast. length_short). Show ();
  5. } Catch (ioexception e ){
  6. E. printstacktrace ();
  7. }

3) Third, use the setwallpaper () method Java code provided in the contextwrapper class

  1. // Override the setwallpaper () method in contextwrapper
  2. Public void setwallpaper (inputstream paraminputstream) throws ioexception {
  3. Super. setwallpaper (paraminputstream );
  4. Toast. maketext (this, "set successfully", 1). Show ();
  5. }
  6. // Set the Wallpaper Code
  7. Resources localresources = getbasecontext (). getresources ();
  8. Inputstream localinputstream2 = localresources
  9. . Openrawresource (getresources (). getidentifier (
  10. "Wallpaper" + imageposition, "drawable", "com. ch "));
  11. Try {
  12. Setwallpaper (localinputstream2 );
  13. } Catch (ioexception e ){
  14. E. printstacktrace ();
  15. }
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

 

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.