添加权限
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
三部分
1.DailyWallpaper.java
2.DailyReceiver.java(BroadcastReceiver)
3.ChangeBgImage.java
DailyWallpaper.java
public class DailyWallpaper extends Activity { private Button save; private Button cancel; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.daily_wallpaper); save = (Button) findViewById(R.id.button1); cancel = (Button) findViewById(R.id.button2); save.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { /* 取得服务启动后几天的0点0分0秒的millsTime */ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); long startTime = calendar.getTimeInMillis(); /* 重复执行的间隔时间 */ long repeatTime = 24 * 60 * 60 * 1000; /* 将更换桌布的排程叵丈AlarmManager中 */ Intent intent = new Intent(DailyWallpaper.this, DailyReceiver.class); PendingIntent sender = PendingIntent.getBroadcast( DailyWallpaper.this, 0, intent, 0); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); /* * setRepeating()让排程重复执行 startTime为开始执行时间 repeatTime为重复执行间隔 * AlarmManager.RTC?使服务休眠时仍然会执行 */ am.setRepeating(AlarmManager.RTC, startTime, repeatTime, sender); Toast.makeText(DailyWallpaper.this, "服务已启动", Toast.LENGTH_SHORT) .show(); /* 启动后马勺因执行几次换桌布的程序以更换今天的桌布 */ Intent intent2 = new Intent(DailyWallpaper.this , ChangeBgImage.class); startActivity(intent2); } }); cancel.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(DailyWallpaper.this, DailyReceiver.class); PendingIntent sender = PendingIntent.getBroadcast(DailyWallpaper.this, 0, intent, 0); /* 由AlarmManager中移除排程 */ AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.cancel(sender); /* 以Toast提示已终止 */ Toast.makeText(DailyWallpaper.this, "服务已终止", Toast.LENGTH_SHORT) .show(); } }); }
DailyReceiver.java(BroadcastReceiver)
public class DailyReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Intent i = new Intent(context, ChangeBgImage.class); Bundle bundleRet = new Bundle(); bundleRet.putString("STR_CALLER", ""); i.putExtras(bundleRet); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } }
ChangeBgImage.java
public class ChangeBgImage extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 取得今天是星期几 */ Calendar ca=Calendar.getInstance(); int dayOfWeek=ca.get(Calendar.DAY_OF_WEEK)-1; Bitmap bitmap = null; switch(dayOfWeek){ case 0: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b01); break; case 1: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b02); break; case 2: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b03); break; case 3: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b04); break; case 4: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b05); break; case 5: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b06); break; case 6: bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b07); break; } try { super.setWallpaper(bitmap); } catch (IOException e) { e.printStackTrace(); } finish(); } }
manifest.xml
<activity android:name=".DailyWallpaper" android:label="@string/app_name"> </activity> <activity android:name=".ChangeBgImage" android:label="@string/app_name"> </activity> <receiver android:name=".DailyReceiver" android:process=":remote" />