使用AlarmManager定时更换壁纸

原文地址:http://blog.csdn.net/u010142437/article/details/9286723

 

更换壁纸要用到WallpaperManager,它提供了clear()方法清除壁纸,还提供了如下方法设置系统的壁纸:

setBitmap(Bitmap bitmap):将壁纸设置为bitmap所代表的位图。

setResource(int resid):将壁纸设置为resid资源所代表的图片。

setStream(InputStream data):将壁纸设置为data数据所代表的图片。

本示例通过AlarmManager来周期性地调用某个Service,从而让系统实现定时更换壁纸的功能。代码如下:

Activity:

  1. package com.guyun.activity; 
  2.  
  3. import com.guyun.alarmchangewallpaper.R; 
  4. import com.guyun.service.ChangeWallpaperService; 
  5.  
  6. import android.app.Activity; 
  7. import android.app.AlarmManager; 
  8. import android.app.PendingIntent; 
  9. import android.app.Service; 
  10. import android.content.Intent; 
  11. import android.os.Bundle; 
  12. import android.util.Log; 
  13. import android.view.View; 
  14. import android.view.View.OnClickListener; 
  15. import android.widget.Button; 
  16. import android.widget.Toast; 
  17.  
  18. publicclass ChangeWallpaperActivity extends Activity { 
  19.     //定义AlarmManager对象 
  20.     private AlarmManager aManager; 
  21. private Button start,stop; 
  22.     @Override 
  23.     protectedvoid onCreate(Bundle savedInstanceState) { 
  24.         super.onCreate(savedInstanceState); 
  25.         setContentView(R.layout.main); 
  26. start=(Button) findViewById(R.id.main_btn_start);
  27.         stop=(Button) findViewById(R.id.main_btn_stop); 
  28.         //获取AlarmManager服务对象 
  29.         aManager=(AlarmManager) getSystemService(Service.ALARM_SERVICE); 
  30.         //指定要启动的Service组件 
  31.         Intent intent=new Intent(ChangeWallpaperActivity.this,ChangeWallpaperService.class); 
  32.         //创建PendingIntent对象 
  33.         final PendingIntent pi=PendingIntent.getService(ChangeWallpaperActivity.this,0, intent, 0); 
  34.         start.setOnClickListener(new OnClickListener() { 
  35.              
  36.             @Override 
  37.             publicvoid onClick(View v) { 
  38.                 Log.i("onClick", "进入onClick"); 
  39.                 //设置每隔3秒执行pi代表的组件一次 
  40.                 aManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 3000, pi); 
  41.                 start.setEnabled(false); 
  42.                 stop.setEnabled(true); 
  43.                 Toast.makeText(ChangeWallpaperActivity.this, "壁纸定时更换启动成功啦", Toast.LENGTH_SHORT).show(); 
  44.             } 
  45.         }); 
  46.         stop.setOnClickListener(new OnClickListener() { 
  47.              
  48.             @Override 
  49.             publicvoid onClick(View v) { 
  50.                 start.setEnabled(true); 
  51.                 stop.setEnabled(false); 
  52.                 //取消对pi的调度 
  53.                 aManager.cancel(pi); 
  54.             } 
  55.         });      
  56.     } 
  57.      
package com.guyun.activity;



import com.guyun.alarmchangewallpaper.R;

import com.guyun.service.ChangeWallpaperService;



import android.app.Activity;

import android.app.AlarmManager;

import android.app.PendingIntent;

import android.app.Service;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;



public class ChangeWallpaperActivity extends Activity {

	//定义AlarmManager对象

	private AlarmManager aManager;

private Button start,stop;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        start=(Button) findViewById(R.id.main_btn_start);

        stop=(Button) findViewById(R.id.main_btn_stop);

        //获取AlarmManager服务对象

        aManager=(AlarmManager) getSystemService(Service.ALARM_SERVICE);

        //指定要启动的Service组件

        Intent intent=new Intent(ChangeWallpaperActivity.this,ChangeWallpaperService.class);

        //创建PendingIntent对象

        final PendingIntent pi=PendingIntent.getService(ChangeWallpaperActivity.this,0, intent, 0);

        start.setOnClickListener(new OnClickListener() {

			

			@Override

			public void onClick(View v) {

				Log.i("onClick", "进入onClick");

				//设置每隔3秒执行pi代表的组件一次

				aManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 3000, pi);

				start.setEnabled(false);

				stop.setEnabled(true);

				Toast.makeText(ChangeWallpaperActivity.this, "壁纸定时更换启动成功啦", Toast.LENGTH_SHORT).show();

			}

		});

        stop.setOnClickListener(new OnClickListener() {

			

			@Override

			public void onClick(View v) {

				start.setEnabled(true);

				stop.setEnabled(false);

				//取消对pi的调度

				aManager.cancel(pi);

			}

		});		

    }

    

}

Service:

  1. package com.guyun.service; 
  2.  
  3. import java.io.IOException; 
  4.  
  5. import com.guyun.alarmchangewallpaper.R; 
  6.  
  7. import android.app.Service; 
  8. import android.app.WallpaperManager; 
  9. import android.content.Intent; 
  10. import android.os.IBinder; 
  11. import android.util.Log; 
  12.  
  13. publicclass ChangeWallpaperService extends Service { 
  14.     // 定义定时更换壁纸的资源 
  15.     int[] wallpapers = newint[] { R.drawable.whatsnew_fornew_01, 
  16.             R.drawable.whatsnew_fornew_02, R.drawable.whatsnew_fornew_03, 
  17.             R.drawable.whatsnew_fornew_04, R.drawable.whatsnew_fornew_05 }; 
  18.     // 定义系统的壁纸管理服务 
  19.     WallpaperManager wManager; 
  20.     // 定义当前所显示的壁纸 
  21.     int current = 0
  22.  
  23.     @Override 
  24.     publicint onStartCommand(Intent intent, int flags, int startId) { 
  25.         Log.i("onStartCommand", "进入onStartCommand"); 
  26.         // 如果到了最后一张重新开始 
  27.         if (current >= wallpapers.length) { 
  28.             current = 0
  29.         } 
  30.         try
  31.             // 改变壁纸 
  32.             wManager.setResource(wallpapers[current++]); 
  33.         } catch (IOException e) { 
  34.             e.printStackTrace(); 
  35.         } 
  36.         returnsuper.onStartCommand(intent, flags, startId); 
  37.     } 
  38.  
  39.     @Override 
  40.     publicvoid onCreate() { 
  41.         super.onCreate(); 
  42.         Log.i("onCreate", "进入onCreate"); 
  43.         // 初始化WallpaperManager对象 
  44.         wManager = WallpaperManager.getInstance(this); 
  45.     } 
  46.  
  47.     @Override 
  48.     public IBinder onBind(Intent intent) { 
  49.         returnnull
  50.     } 
package com.guyun.service;



import java.io.IOException;



import com.guyun.alarmchangewallpaper.R;



import android.app.Service;

import android.app.WallpaperManager;

import android.content.Intent;

import android.os.IBinder;

import android.util.Log;



public class ChangeWallpaperService extends Service {

	// 定义定时更换壁纸的资源

	int[] wallpapers = new int[] { R.drawable.whatsnew_fornew_01,

			R.drawable.whatsnew_fornew_02, R.drawable.whatsnew_fornew_03,

			R.drawable.whatsnew_fornew_04, R.drawable.whatsnew_fornew_05 };

	// 定义系统的壁纸管理服务

	WallpaperManager wManager;

	// 定义当前所显示的壁纸

	int current = 0;



	@Override

	public int onStartCommand(Intent intent, int flags, int startId) {

		Log.i("onStartCommand", "进入onStartCommand");

		// 如果到了最后一张重新开始

		if (current >= wallpapers.length) {

			current = 0;

		}

		try {

			// 改变壁纸

			wManager.setResource(wallpapers[current++]);

		} catch (IOException e) {

			e.printStackTrace();

		}

		return super.onStartCommand(intent, flags, startId);

	}



	@Override

	public void onCreate() {

		super.onCreate();

		Log.i("onCreate", "进入onCreate");

		// 初始化WallpaperManager对象

		wManager = WallpaperManager.getInstance(this);

	}



	@Override

	public IBinder onBind(Intent intent) {

		return null;

	}

}

加上如下权限:

  1. <uses-permissionandroid:name="android.permission.SET_WALLPAPER"/> 

你可能感兴趣的:(manager)