1. push the background image to the SDCard adb push MM-320x480.png/sdcard/to confirm whether it already exists, you can go to SDCard to see: adb shellcd sdcardls can see the uploaded picture: 2. Compile the Activity program
Public static final String TAG = "WallpaperActivity ";
/**
* Background image name
*/
Private static final String FILE_NAME = "MM-320x480.png ";
/**
* @ See android. app. Activity # onCreate (android. OS. Bundle)
*/
Public void onCreate (Bundle cycle ){
Super. onCreate (cycle );
Super. setContentView (R. layout. wallpaper );
// Obtain the background image
Bitmap wallpaper = this. getWallpager ();
// Set the desktop background
This. putWallpaper (wallpaper );
}
/**
* Obtain the background image in the SDCard.
*/
Private Bitmap getWallpager (){
// SDCard path, that is, "/sdcard /"
File root = Environment. getExternalStorageDirectory ();
// Mmimage
File wall = new File (root, FILE_NAME );
// Mmimage path
String path = wall. getAbsolutePath ();
Log. d (TAG, "MM file path:" + path );
Return BitmapFactory. decodeFile (path );
}
/**
* Set the desktop background
*/
Private void putWallpaper (Bitmap bitmap ){
Try {
WallpaperManager wallpaperManager = WallpaperManager. getInstance (this );
WallpaperManager. setBitmap (bitmap );
} Catch (IOException e ){
String msg = "An exception occurred when setting the desktop background:" + e. getLocalizedMessage ();
Log. e (TAG, "an exception occurred when setting the desktop background! ", E );
Toast. makeText (this, msg, Toast. LENGTH_LONG). show ();
}
}
3. Add the permission to set the desktop background in the AndroidManifest. xml file:
<! -- Set the desktop background permission -->
<Uses-permission android: name = "android. permission. SET_WALLPAPER"/>
4. Run the program and return to the desktop. The background has changed.