温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

WallpaperManager如何在Android 中使用

发布时间:2021-04-01 17:29:39 来源:亿速云 阅读:323 作者:Leah 栏目:移动开发

这篇文章给大家介绍WallpaperManager如何在Android 中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

Android 中WallpaperManager用法实例

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

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

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

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

关于WallpaperManager如何在Android 中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI