Android 之 WallpaperManager用法

[color=olive][size=medium][b]
注意:壁纸的设置得加入权限:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>

1、WallpaperManager 对象的获得:wallpaperManager =WallpaperManager.getInstance(this);

2、设置壁纸的方法:
方法一:wallpaperManager.setBitmap(); // 参数
方法二:wallpaperManager.setResource(); // 参数为资源ID
方法三:通过ContextWrapper 类中的setWallpaper(); 方法 // 参数为一个输入流

3、定时更换壁纸:
使用 AlarmManager 系统定时服务
PendingIntent pi = PendingIntent.getService(this,0, new Intent("SERVICE_TO_SETWALL"), PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
// 类型 ,执行延迟的时间,实行时间间隔,动作
alarmManager.setRepeating(alarmManager.RTC_WAKEUP, 0, 2000, pi);[/b][/size][/color]


[size=medium][color=red][b]下列为一个服务用来设置墙纸:[/b][/color][/size]


import android.app.Service;
import android.app.WallpaperManager;
import android.content.Intent;
import android.os.IBinder;


/**
* 实现效果 -- 墙纸的切换 , 背景图片
* @author Administrator
*
*/
public class WallService extends Service {

private int[] res = new int[]{R.drawable.a,R.drawable.b,R.drawable.c}; // 切换图片资源
private WallpaperManager wallpaperManager; //墙纸管理器
private int index; // 资源索引

// 绑定服务
public IBinder onBind(Intent intent) {

return null;
}

// 创建服务
public void onCreate() {
super.onCreate();
wallpaperManager = WallpaperManager.getInstance(WallService.this); // 获取壁纸管理器对象
}

// 销毁服务
public void onDestroy() {
super.onDestroy();

}

/**
* 启动服务
* 每次启动开始获取资源
*/
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
try{
if(index>=3){
index = 0;
}
wallpaperManager.setResource(res[index++]); // 设置资源
}catch(Exception ex){
ex.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值