Introduction to this section:
This section brings you WallpaperManager. As its name suggests, it is an API related to phone wallpapers. In this section, we will describe the basic usage of WallpaperManager, call the system's built-in wallpaper selection feature, set the Activity's background to the wallpaper background, and write an example of changing wallpaper on a timer~ Alright, no more rambling, let's start the content of this section~
Official API Documentation:WallpaperManager
1. Basic usage of WallpaperManager
Related methods
Related methods for setting wallpaper:
- setBitmap(Bitmap bitmap): Set the wallpaper to the bitmap represented by the bitmap
- setResource(int resid): Set the wallpaper to the image represented by the resid resource
- setStream(InputStream data): Set the wallpaper to the image represented by the data
Other methods:
- clear(): Clear the wallpaper and set it back to the system default wallpaper
- getDesiredMinimumHeight(): Minimum wallpaper height
- getDesiredMinimumWidth(): Minimum wallpaper width
- getDrawable(): Get the current system wallpaper; if no wallpaper is set, return the system default wallpaper
- getWallpaperInfo(): If the current wallpaper is a live wallpaper, return the live wallpaper information
- peekDrawable(): Get the current system wallpaper; return null if no wallpaper is set
Obtaining the WallpaperManager object
WallpaperManager wpManager =WallpaperManager.getInstance(this);
Permission required for setting wallpaper
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
2. Calling the system's built-in wallpaper selection feature
Button btn_set = (Button) findViewById(R.id.btn_set);
btn_set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent chooseIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivity(Intent.createChooser(chooseIntent, "选择壁纸"));
}
});
Running effect screenshot:

3. Setting the Activity's background to the wallpaper background
There are two methods: one is to set it with code in the Activity, the other is to modify the Activity's theme in AndroidManifest.xml~!

Method 1: Set in the Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme_Wallpaper_NoTitleBar_Fullscreen);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Method 2: Modify the theme in AndroidManifest.xml:
<activity android:name=".MainActivity" android:theme="@android:style/Theme.Wallpaper.NoTitleBar"/>
4. Demo of changing wallpaper on a timer
Here we use the AlarmManager (alarm service) learned earlier. If you are not familiar with it, you can go to:10.5 AlarmManager (Alarm Service)to learn~ Now let's write a Demo~
Running effect screenshot:

Code implementation:
First, let's write a Service that changes the wallpaper on a timer:WallPaperService.java
/**
* Created by Jay on 2015/11/13 0013.
*/
public class WallPaperService extends Service {
private int current = 0; //当前壁纸下标
private int[] papers = new int[]{R.mipmap.gui_1,R.mipmap.gui_2,R.mipmap.gui_3,R.mipmap.gui_4};
private WallpaperManager wManager = null; //定义WallpaperManager服务
@Override
public void onCreate() {
super.onCreate();
wManager = WallpaperManager.getInstance(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(current >= 4)current = 0;
try{
wManager.setResource(papers[current++]);
}catch(Exception e){e.printStackTrace();}
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Next, let's write a simple layout with three Buttons:activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启自动换壁纸" />
<Button
android:id="@+id/btn_off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭自动换壁纸" />
<Button
android:id="@+id/btn_clean"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清除壁纸" />
</LinearLayout>
Next is our Activity, here we instantiate aManager and set up the scheduled events~:MainActivity.java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_on;
private Button btn_off;
private Button btn_clean;
private AlarmManager aManager;
private PendingIntent pi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//①获得AlarmManager对象:
aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//②指定要启动的Service,并指明动作是Servce:
Intent intent = new Intent(MainActivity.this, WallPaperService.class);
pi = PendingIntent.getService(MainActivity.this, 0, intent, 0);
bindViews();
}
private void bindViews() {
btn_on = (Button) findViewById(R.id.btn_on);
btn_off = (Button) findViewById(R.id.btn_off);
btn_clean = (Button) findViewById(R.id.btn_clean);
btn_on.setOnClickListener(this);
btn_off.setOnClickListener(this);
btn_clean.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_on:
aManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 3000, pi);
btn_on.setEnabled(false);
btn_off.setEnabled(true);
Toast.makeText(MainActivity.this, "自动更换壁纸设置成功", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_off:
btn_on.setEnabled(true);
btn_off.setEnabled(false);
aManager.cancel(pi);
break;
case R.id.btn_clean:
try {
WallpaperManager.getInstance(getApplicationContext()).clear();
Toast.makeText(MainActivity.this, "清除壁纸成功~", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
Finally, don't forget to add the wallpaper setting permission and register our Service:AndroidManifest.xml:
<uses-permission android:name="android.permission.SET_WALLPAPER" /> <service android:name=".WallPaperService"/>
Okay, it's very simple~
5. Download of example code for this section
WallpaperManagerDemo.zip
Summary of this section:
Alright, this section introduced some basic usages of WallpaperManager~ More things still need you to explore on your own~
Thank you~!
Thank you~!