想起来以前做的一个需求,就来总结一下,做成的效果就是每几秒中在Launcher中更改一次背景,是一个demo。正好Launcher模块中有更改背景这个功能,就可以借鉴以下。
更换壁纸代码不复杂,就在Launcher模块中的WallpaperChooser类。
主要代码如下:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//设置窗口无标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//添加资源墙纸
findWallpapers();
setContentView(R.layout.wallpaper_chooser);
//Gallary控件实现查看图片
mGallery = (Gallery) findViewById(R.id.gallery);
mGallery.setAdapter(new ImageAdapter(this));
mGallery.setOnItemSelectedListener(this);
mGallery.setCallbackDuringFling(false);
findViewById(R.id.set).setOnClickListener(this);
mImageView = (ImageView) findViewById(R.id.wallpaper);
}
private void findWallpapers() {
mThumbs = new ArrayList<Integer>(24);
mImages = new ArrayList<Integer>(24);
//Return a Resources instance for your application's package.
final Resources resources = getResources();
// Context.getPackageName() may return the "original" package name,
// com.android.launcher2; Resources needs the real package name,
// com.android.launcher. So we ask Resources for what it thinks the
// package name should be.
final String packageName = resources.getResourcePackageName(R.array.wallpapers);
addWallpapers(resources, packageName, R.array.wallpapers);
addWallpapers(resources, packageName, R.array.extra_wallpapers);
}
private void addWallpapers(Resources resources, String packageName, int list) {
//返回与特定的资源ID相关联的字符串数组
final String[] extras = resources.getStringArray(list);
for (String extra : extras) {
Log.v("-------extra------",extra);
//获取到资源ID
int res = resources.getIdentifier(extra, "drawable", packageName);
if (res != 0) {
final int thumbRes = resources.getIdentifier(extra + "_small",
"drawable", packageName);
if (thumbRes != 0) {
mThumbs.add(thumbRes);
mImages.add(res);
}
}
}
}
。。。
。。。
private void selectWallpaper(int position) {
if (mIsWallpaperSet) {
return;
}
mIsWallpaperSet = true;
try {
//获取 WALLPAPER_SERVICE的系统服务
WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
wpm.setResource(mImages.get(position));
setResult(RESULT_OK);
finish();
} catch (IOException e) {
Log.e(TAG, "Failed to set wallpaper: " + e);
}
}
这块就是Launcher更改壁纸的代码,再针对我们的需求,比较好解决了。
可以在Launcher.java这个类中增加上面的几个函数和所需的变量,然后再写个TimerTask每几秒发送一个Message,最后在Handler中获取WallpaperManager服务,然后更改壁纸。增加代码如下:
private Timer timer = new Timer();
static int count = 0;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int msgId = msg.what;
switch (msgId) {
case 1:
WallpaperManager wpm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
try {
wpm.setResource(mImages.get(count));
count++;
if(count == 10){
count = 0;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
。。。
。。。
private void setTimerTask() {
timer.schedule(new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
}, 1000, 3000);
}
。。。
。。。
@Override
public void onDestroy() {
super.onDestroy();
。。。
timer.cancel();
}
再Launcher.java文件里OnCreate中合适的地方调用setTimerTask函数即可实现这种感觉怪怪的需求了。