更换壁纸要用到WallpaperManager,它提供了clear()方法清除壁纸,还提供了如下方法设置系统的壁纸:
setBitmap(Bitmap bitmap):将壁纸设置为bitmap所代表的位图。
setResource(int resid):将壁纸设置为resid资源所代表的图片。
setStream(InputStream data):将壁纸设置为data数据所代表的图片。
本示例通过AlarmManager来周期性地调用某个Service,从而让系统实现定时更换壁纸的功能。代码如下:
Activity:
package com.guyun.activity; import com.guyun.alarmchangewallpaper.R; import com.guyun.service.ChangeWallpaperService; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class ChangeWallpaperActivity extends Activity { //定义AlarmManager对象 private AlarmManager aManager; private Button start,stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); start=(Button) findViewById(R.id.main_btn_start); stop=(Button) findViewById(R.id.main_btn_stop); //获取AlarmManager服务对象 aManager=(AlarmManager) getSystemService(Service.ALARM_SERVICE); //指定要启动的Service组件 Intent intent=new Intent(ChangeWallpaperActivity.this,ChangeWallpaperService.class); //创建PendingIntent对象 final PendingIntent pi=PendingIntent.getService(ChangeWallpaperActivity.this,0, intent, 0); start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.i("onClick", "进入onClick"); //设置每隔3秒执行pi代表的组件一次 aManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 3000, pi); start.setEnabled(false); stop.setEnabled(true); Toast.makeText(ChangeWallpaperActivity.this, "壁纸定时更换启动成功啦", Toast.LENGTH_SHORT).show(); } }); stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { start.setEnabled(true); stop.setEnabled(false); //取消对pi的调度 aManager.cancel(pi); } }); } }
Service:
package com.guyun.service; import java.io.IOException; import com.guyun.alarmchangewallpaper.R; import android.app.Service; import android.app.WallpaperManager; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class ChangeWallpaperService extends Service { // 定义定时更换壁纸的资源 int[] wallpapers = new int[] { R.drawable.whatsnew_fornew_01, R.drawable.whatsnew_fornew_02, R.drawable.whatsnew_fornew_03, R.drawable.whatsnew_fornew_04, R.drawable.whatsnew_fornew_05 }; // 定义系统的壁纸管理服务 WallpaperManager wManager; // 定义当前所显示的壁纸 int current = 0; @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("onStartCommand", "进入onStartCommand"); // 如果到了最后一张重新开始 if (current >= wallpapers.length) { current = 0; } try { // 改变壁纸 wManager.setResource(wallpapers[current++]); } catch (IOException e) { e.printStackTrace(); } return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); Log.i("onCreate", "进入onCreate"); // 初始化WallpaperManager对象 wManager = WallpaperManager.getInstance(this); } @Override public IBinder onBind(Intent intent) { return null; } }
加上如下权限:
<uses-permission android:name="android.permission.SET_WALLPAPER" />
本程序还可以和之前做过的SD卡文件浏览器结合起来,让用户可以添加SD卡中的图片作为可供更换的壁纸图片。