Android 中WallpaperManager用法实例

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

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

时间: 2024-09-16 22:13:41

Android 中WallpaperManager用法实例的相关文章

Java中的instanceof关键字在Android中的用法实例详解_java

在下面介绍Android中如何使用instanceof关键字开发更方便时,先来温习一下java中instanceof的概念. instanceof大部分的概念是这样定义的:instanceof是Java的一个二元操作符,和==,>,<是同一类东西.由于它是由字母组成的,所以也是Java的保留关键字.它的作用是测试它左边的对象是否是它右边的类的实例,返回boolean类型的数据.举个栗子: String s = "I AM an Object!"; boolean isObj

Android中AlertDialog用法实例分析_Android

本文实例分析了Android中AlertDialog用法,分享给大家供大家参考,具体如下: Android中AlertDialog为一些程序提供了对话框,有些功能能够进一步满足程序的需要.下面举例介绍. 程序如下: import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.

Android中TelephonyManager用法实例_Android

本文实例讲述了Android中TelephonyManager用法.分享给大家供大家参考,具体如下: 一.概述: TelephonyManager类主要提供了一系列用于访问与手机通讯相关的状态和信息的get方法.其中包括手机SIM的状态和信息.电信网络的状态及手机用户的信息.在应用程序中可以使用这些get方法获取相关数据. TelephonyManager类的对象可以通过Context.getSystemService(Context.TELEPHONY_SERVICE)方法来获得,需要注意的是

Android中Notification用法实例总结_Android

本文实例总结了 Android中Notification用法.分享给大家供大家参考,具体如下: 我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们也知道Android系统也是在不断升级的,有关Notification的用法也就有很多种,有的方法已经被android抛弃了,现在我实现了三种不同的方法,并适应不同的android版本.现在我就把代码公布出

Android中OptionMenu用法实例_Android

本文实例讲述了Android中OptionMenu用法.分享给大家供大家参考.具体如下: 1.无需设置布局文件 2.创建一个类,基础Activity 3.重写onOptionsItemSelected方法,在这个方法里面添加菜单项 4.如果需要给菜单项添加单击事件,需要重写onOptionsItemSelected方法 MenuDemoActivity.java如下: package com.yyl; import Android.app.Activity; import Android.os.

Android中ImageView用法实例分析_Android

本文实例分析了Android中ImageView用法.分享给大家供大家参考,具体如下: 猜牌游戏大家可能以前都玩过,这里我们用这个小游戏来说明ImageView的用法. 首先,在res/drawable中引入三张牌:分别是梅花7,梅花8,梅花9 然后在res/layout/main.xml中配置一个TextView,三个ImageView以及一个Button <?xml version="1.0" encoding="utf-8"?> <Linea

Android中ListView用法实例分析_Android

本文实例分析了Android中ListView用法.分享给大家供大家参考,具体如下: 通过在Layout中添加ListView Widget可以达到在页面布局具有列表效果的交互页面.在这里通过举例来说明怎样在Layout中添加ListView以及怎样应用. 配合设计了两个事件Listener:  OnItemSelectedListener事件为鼠标的滚轮转动时所选择的值:OnItemClickListener事件则为当鼠标单击时,所触发的事件.由此可以区别出list中的"选择"与&q

Android中ListActivity用法实例分析_Android

本文实例分析了Android中ListActivity用法.分享给大家供大家参考,具体如下: 程序如下: import android.app.ListActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widge

Android中Bitmap用法实例分析_Android

本文实例讲述了Android中Bitmap用法.分享给大家供大家参考,具体如下: 一般在android程序中把图片文件放在res/drawable目录下就可以通过R.drawable.id来使用,但在存储卡中的图片怎样引用呢?下面通过实现这个功能来介绍Bitmap的用法. 程序如下: import java.io.File; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.B