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
- Package Xiaosi. wallpaper;
- Import java.io.IOException;
- Import android.app.Activity;
- Import Android.app.WallpaperManager;
- Import android.content.res.Resources;
- Import Android.graphics.Bitmap;
- Import Android.graphics.BitmapFactory;
- Import Android.os.Bundle;
- Import Android.widget.Toast;
- Public class Wallpaperactivity extends Activity {
- /** Called when the activity is first created. * /
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Wallpapermanager Wallpapermanager = wallpapermanager.getinstance (this);
- Resources res = getresources ();
- Bitmap Bitmap=bitmapfactory.decoderesource (res,r.drawable.h);
- Try
- {
- Wallpapermanager.setbitmap (bitmap);
- }
- catch (IOException e)
- {
- E.printstacktrace ();
- }
- }
The second method: through the Setresource () in the Wallpapermanager method
[Java]View PlainCopy
- wallpapermanager wallpapermanager = Wallpapermanager.getinstance (this);
- < span class= "keyword" >try {
- wallpapermanager.setresource (Getresources (). Getidentifier ( "wallpaper" + imageposition, "drawable", "com.ch");
- toast.maketext (this,
- &NBSP;&NBSP;&NBSP;&NBSP;} catch (ioexception e) {
- e.printstacktrace ();
- }&NBSP;&NBSP;
Third method: Through the SetWallpaper () method provided in the Contextwrapper class
[Java]View PlainCopy
- 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 ();
- }
Android Learning notes advanced 21 settings Wallpaper