栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android 中WallpaperManager用法实例

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 中WallpaperManager用法实例

Android 中WallpaperManager用法实例

注意:壁纸的设置得加入权限:

 

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); 

下列为一个服务用来设置墙纸:


import android.app.Service; 
import android.app.WallpaperManager; 
import android.content.Intent; 
import android.os.IBinder; 
 
 
 
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(); 
    } 
  } 
} 

以上就是Android  WallpaperManager的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/157926.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号