日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

我的Android进阶之旅------gt;Android使用AlarmManager全局定时器实现定时更换壁纸

發布時間:2025/7/14 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 我的Android进阶之旅------gt;Android使用AlarmManager全局定时器实现定时更换壁纸 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? ? ??

? 該DEMO將會通過AlarmManager來周期的調用ChangeService,從而讓系統實現定時更換壁紙的功能。

更換壁紙的API為android.app.WallpaperManager,它提供了clear()方法來清除壁紙,還提供了如下方法來設置壁紙。

setResource(int resid)將壁紙設置為resid資源所代表的圖片

setBitmap(Bitmap bitmap)將壁紙設置為bitmap所代表的位圖

setStream(InputStream data)將壁紙設置為data數據所代表的圖片



Android中的AlarmManager實質上是一個全局的定時器,是Android中常用的一種系統級

別的提示服務,在指定時間或周期性啟動其它組件

(包括Activity,Service,BroadcastReceiver)。

一、概述:

該類提供一種訪問系統鬧鐘服務的方式,允許你去設置在將來的某個時間點去執行你的應用程序。當你的鬧鐘響起(時間到)時,在它上面注冊的一個意圖(Intent)將會被系統以廣播發出,然后自動啟動目標程序,如果它沒有正在運行。注冊的鬧鐘會被保留即使設備處于休眠中(如果鬧鐘在給定時間響起可以選擇是否喚醒設備)。如果鬧鐘關閉或者重啟,鬧鐘將被清除。

只要廣播的onReceive()方法正在執行,這鬧鐘管理者(AlarmManager)會持有一個CPU喚醒鎖,這是為了保證手機不會休眠直到處理完該廣播,一旦onReceive()返回,那么鬧鐘管理者將會釋放喚醒鎖。這意味著只要OnReceive()方法完成,你的手機可能在某些情況下進入休眠,如果你的鬧鐘廣播接收者調用的是Context.startService(),那么手機有可能在被請求的服務執行之前進入休眠,為了防止這種情況,你的BroadcastReceiver和服務需要實現一個單獨的喚醒鎖策略以確保手機繼續運行,直到服務可用。

此處注意:該類適用于你想讓應用程序在將來某個指定時間點執行的情況,即使你的應用程序現在沒有運行。對一般的時間操作,使用Handler是更容易和更有效率的

二、公有方法(Public Methods):

void cancel(PendingIntent operation)

取消AlarmManager的定時服務。


void set(inttype,longtriggerAtTime, PendingIntent operation)

設置在triggerAtTime時間啟動由operation參數指定的組件。(該方法用于設置一次性鬧鐘)


void setInexactRepeating(inttype,longtriggerAtTime,longinterval, PendingIntent operation)

設置一個非精確的周期性任務。


void setRepeating(inttype,longtriggerAtTime,longinterval, PendingIntent operation)? ? ? ?設置一個周期性執行的定時服務。


void setTime(longmillis)

設置系統“墻”時鐘。需要android.permission.SET_TIME.權限。


void setTimeZone(String timeZone) 設置系統的默認時區。需要android.permission.SET_TIME_ZONE.權限。


三、常用方法說明:
AlarmManager的常用方法有三個:


set(inttype,longstartTime,PendingIntent pi)


該方法用于設置一次性鬧鐘。
第一個參數int type指定定時服務的類型,該參數接受如下值:

ELAPSED_REALTIME: 在指定的延時過后,發送廣播,但不喚醒設備(鬧鐘在睡眠狀態下不可用)。如果在系統休眠時鬧鐘觸發,它將不會被傳遞,直到下一次設備喚醒。

ELAPSED_REALTIME_WAKEUP: 在指定的延時過后,發送廣播,并喚醒設備(即使關機也會執行operation所對應的組件) 。
延時是要把系統啟動的時間SystemClock.elapsedRealtime()算進去的,具體用法看代碼。

RTC: 指定當系統調用System.currentTimeMillis()方法返回的值與triggerAtTime相等時啟動operation所對應的設備(在指定的時刻,發送廣播,但不喚醒設備)。如果在系統休眠時鬧鐘觸發,它將不會被傳遞,直到下一次設備喚醒(鬧鐘在睡眠狀態下不可用)。

RTC_WAKEUP: 指定當系統調用System.currentTimeMillis()方法返回的值與triggerAtTime相等時啟動operation所對應的設備(在指定的時刻,發送廣播,并喚醒設備)。即使系統關機也會執行 operation所對應的組件。

第二個參數表示鬧鐘執行時間。

第三個參數PendingIntent pi表示鬧鐘響應動作:

PendingIntent pi:是鬧鐘的執行動作,比如發送一個廣播、給出提示等等。PendingIntent是Intent的封裝類。需要注意的是,如果是通過啟動服務來實現鬧鐘提示的話,PendingIntent對象的獲取就應該采用Pending.getService(Context c,int i,Intentintent,int j)方法;如果是通過廣播來實現鬧鐘提示的話,PendingIntent對象的獲取就應該采用PendingIntent.getBroadcast(Context c,inti,Intent intent,int j)方法;如果是采用Activity的方式來實現鬧鐘提示的話,PendingIntent對象的獲取就應該采用PendingIntent.getActivity(Context c,inti,Intent intent,int j)方法。如果這三種方法錯用了的話,雖然不會報錯,但是看不到鬧鐘提示效果。


setRepeating(inttype,longstartTime,longintervalTime,PendingIntent pi)

設置一個周期性執行的定時服務。第一個參數表示鬧鐘類型,第二個參數表示鬧鐘首次執行時間,第三個參數表示鬧鐘兩次執行的間隔時間,第三個參數表示鬧鐘響應動作。


setInexactRepeating(int type, long triggerAtMillis,long intervalMillis,PendingIntent operation)

該方法也用于設置重復鬧鐘,與第二個方法相似,不過其兩個鬧鐘執行的間隔時間不是固定的而已。它相對而言更省電(power-efficient)一些,因為系統可能會將幾個差不多的鬧鐘合并為一個來執行,減少設備的喚醒次數。第三個參數intervalTime為鬧鐘間隔,內置的幾個變量如下:

INTERVAL_DAY:????? 設置鬧鐘,間隔一天
INTERVAL_HALF_DAY:? 設置鬧鐘,間隔半天
INTERVAL_FIFTEEN_MINUTES:設置鬧鐘,間隔15分鐘
INTERVAL_HALF_HOUR:???? 設置鬧鐘,間隔半個小時
INTERVAL_HOUR:? 設置鬧鐘,間隔一個小時


==================================================================================================


? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

AlarmChangeWallpaper.java

package com.oyp.alarm.change.wallpaper;import android.os.Bundle; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast;public class AlarmChangeWallpaper extends Activity {AlarmManager alarmManager;Button start, stop;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);start = (Button) findViewById(R.id.start);stop = (Button) findViewById(R.id.stop);alarmManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);// 指定啟動ChangeService組件Intent intent = new Intent(AlarmChangeWallpaper.this,ChangeService.class);final PendingIntent pi = PendingIntent.getService(AlarmChangeWallpaper.this, 0, intent, 0);start.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//設置鬧鐘從當前時間開始,每隔5s執行一次PendingIntent對象pi,注意第一個參數與第二個參數的關系 // 5秒后通過PendingIntent pi對象發送廣播 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 5000, pi);start.setEnabled(false);stop.setEnabled(true);Toast.makeText(AlarmChangeWallpaper.this, "壁紙定時更換成功",Toast.LENGTH_SHORT).show();}});stop.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {start.setEnabled(true);stop.setEnabled(false);//取消對pi的調度alarmManager.cancel(pi);}});} }


ChangeService.java

package com.oyp.alarm.change.wallpaper;import android.app.Service; import android.app.WallpaperManager; import android.content.Intent; import android.os.IBinder;public class ChangeService extends Service {//定義定時更換的壁紙資源int[] wallpapers = new int[] { R.drawable.a, R.drawable.b, R.drawable.c,R.drawable.d };//定義系統的壁紙管理服務WallpaperManager wallpaperManager;//定義當前所顯示的壁紙int current = 0;@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {//如果到了最后一張,重新開始if (current >=wallpapers.length) {current = 0;}try {//更換壁紙wallpaperManager.setResource(wallpapers[current++]);} catch (Exception e) {e.printStackTrace();}return START_STICKY;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();//初始化WallpaperManagerwallpaperManager=WallpaperManager.getInstance(this);}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".AlarmChangeWallpaper" ><Buttonandroid:id="@+id/start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Start" /><Buttonandroid:id="@+id/stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Stop" /></LinearLayout>



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.oyp.alarm.change.wallpaper"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="18" /><!-- 授予用戶修改壁紙的權限 --><uses-permission android:name="android.permission.SET_WALLPAPER" /><applicationandroid:allowBackup="true"android:icon="@drawable/c"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.oyp.alarm.change.wallpaper.AlarmChangeWallpaper"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!-- 注冊 ChangeService --><service android:name=".ChangeService" /></application> </manifest>


PS:可以參考一下鏈接

http://blog.csdn.net/fengyuzhengfan/article/details/38417935?utm_source=tuicool

主要實現了:

1.使用AssetManager將assets目錄中的文件復制到SD卡的指定位置

2.使用AlarmManager全局定時器,周期性的啟動指定組件切換壁紙

3.使用SharedPreferences,將用戶個性化的設置保存到手機(例如壁紙切換頻率)

4.使用自定義標題欄

5.使用了GestureDetector手勢檢測器,允許用戶滑動切屏

6.使用了overridePendingTransition,在切屏的時候有動畫效果



?


??????????????????????????? ====================================================================================

? 作者:歐陽鵬? 歡迎轉載,與人分享是進步的源泉!

? 轉載請保留原文地址:http://blog.csdn.net/ouyang_peng

====================================================================================


總結

以上是生活随笔為你收集整理的我的Android进阶之旅------gt;Android使用AlarmManager全局定时器实现定时更换壁纸的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。