Android Learning notes advanced 21 settings Wallpaper

Source: Internet
Author: User

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

<uses-permission android:name = "Android.permission.SET_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> invokes the SetBitmap () method in a singleton class by instantiating the Wallpapermanager class.

[Java]View PlainCopy
  1. Package Xiaosi. wallpaper;
  2. Import java.io.IOException;
  3. Import android.app.Activity;
  4. Import Android.app.WallpaperManager;
  5. Import android.content.res.Resources;
  6. Import Android.graphics.Bitmap;
  7. Import Android.graphics.BitmapFactory;
  8. Import Android.os.Bundle;
  9. Import Android.widget.Toast;
  10. Public class Wallpaperactivity extends Activity {
  11. /** Called when the activity is first created. * /
  12. @Override
  13. public void OnCreate (Bundle savedinstancestate) {
  14. super.oncreate (savedinstancestate);
  15. Setcontentview (R.layout.main);
  16. Wallpapermanager Wallpapermanager = wallpapermanager.getinstance (this);
  17. Resources res = getresources ();
  18. Bitmap Bitmap=bitmapfactory.decoderesource (res,r.drawable.h);
  19. Try
  20. {
  21. Wallpapermanager.setbitmap (bitmap);
  22. }
  23. catch (IOException e)
  24. {
  25. E.printstacktrace ();
  26. }
  27. }

The second method: through the Setresource () in the Wallpapermanager method

[Java]View PlainCopy
    1. wallpapermanager wallpapermanager =  Wallpapermanager.getinstance (this);   
    2.     < span class= "keyword" >try {  
    3.          wallpapermanager.setresource (Getresources (). Getidentifier ( "wallpaper"  +  imageposition,  "drawable",   "com.ch");  
    4. toast.maketext (this, 
    5. &NBSP;&NBSP;&NBSP;&NBSP;}  catch  (ioexception e)  {  
    6.          e.printstacktrace ();   
    7. }&NBSP;&NBSP;

Third method: Through the SetWallpaper () method provided in the Contextwrapper class

[Java]View PlainCopy
  1. Overriding the SetWallpaper () method in Contextwrapper
  2. Public void SetWallpaper (InputStream paraminputstream) throws IOException {
  3. Super.setwallpaper (Paraminputstream);
  4. Toast.maketext (This, "set Success", 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. }

Android Learning notes advanced 21 settings Wallpaper

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.