當前位置:首頁 » 安卓系統 » androidwakelock

androidwakelock

發布時間: 2025-02-14 09:49:45

『壹』 android 使用timertask和alarmmanager哪個好點

在消息的獲取上是選擇輪詢還是推送得根據實際的業務需要來技術選型,例如對消息實時性比較高的需求,比如微博新通知或新聞等那就最好是用推送了。但如果只是一般的消息檢測比如更新檢查,可能是半個小時或一個小時一次,那用輪詢也是一個不錯的選擇,因為不需要額外搭建推送伺服器,不用額外配置推送服務。另外推送現在一般以維持長連接的方式實現,在手機客戶端也會耗費一定的電量。今天就介紹一個在Android上實現輪詢機制的方法——使用AlarmManager
AlarmManager在Android中主要用來定時處理一個事件或是定期處理一個事件,比如鬧鍾應用就是使用AlarmManager來實現的,我們今天要使用AlarmManager的定期執行功能來實現輪詢的功能。對於定期執行任務也可以用Timer和TimerTask來實現,也可以開一個Service在Thread裡面以while循環來實現。但最好的方案還是選用AlarmManager,這里涉及一個Android系統鎖的機制,即系統在檢測到一段時間沒有活躍以後,會關閉一些不必要的服務來減少資源和電量消耗。使用Timer和Service來實現的話很可能出現的情況就是屏幕熄滅後一段時間,服務就被停止了,當然輪詢也就被停止了。這個大家可以實驗一下,之前我寫過一篇文章也介紹了一種保持後台喚醒的機制《使用WakeLock使Android應用程序保持後台喚醒》,感興趣的可以看看。那麼接下來就開始使用AlarmManager+Service+Thread來實現我們的輪詢服務吧!

一、新建輪詢工具類PollingUtils.java

[java]view plain
publicclass PollingUtils {

//開啟輪詢服務
publicstaticvoid startPollingService(Context context, int seconds, Class<?> cls,String action) {
//獲取AlarmManager系統服務
AlarmManager manager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);

//包裝需要執行Service的Intent
Intent intent = new Intent(context, cls);
intent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);

//觸發服務的起始時間
long triggerAtTime = SystemClock.elapsedRealtime();

//使用AlarmManger的setRepeating方法設置定期執行的時間間隔(seconds秒)和需要執行的Service
manager.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtTime,
seconds * 1000, pendingIntent);
}

//停止輪詢服務
publicstaticvoid stopPollingService(Context context, Class<?> cls,String action) {
AlarmManager manager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, cls);
intent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
//取消正在執行的服務
manager.cancel(pendingIntent);
}
}

二、構建輪詢任務執行PollingService.java

[java]view plain
publicclass PollingService extends Service {

publicstaticfinal String ACTION = "com.ryantang.service.PollingService";

private Notification mNotification;
private NotificationManager mManager;

@Override
public IBinder onBind(Intent intent) {
returnnull;
}

@Override
publicvoid onCreate() {
initNotifiManager();
}

@Override
publicvoid onStart(Intent intent, int startId) {
new PollingThread().start();
}

//初始化通知欄配置
privatevoid initNotifiManager() {
mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
mNotification = new Notification();
mNotification.icon = icon;
mNotification.tickerText = "New Message";
mNotification.defaults |= Notification.DEFAULT_SOUND;
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
}

//彈出Notification
privatevoid showNotification() {
mNotification.when = System.currentTimeMillis();
//Navigator to the new activity when click the notification title
Intent i = new Intent(this, MessageActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i,
Intent.FLAG_ACTIVITY_NEW_TASK);
mNotification.setLatestEventInfo(this,
getResources().getString(R.string.app_name), "You have new message!", pendingIntent);
mManager.notify(0, mNotification);
}

/**
* Polling thread
* 模擬向Server輪詢的非同步線程
* @Author Ryan
* @Create 2013-7-13 上午10:18:34
*/
int count = 0;
class PollingThread extends Thread {
@Override
publicvoid run() {
System.out.println("Polling...");
count ++;
//當計數能被5整除時彈出通知
if (count % 5 == 0) {
showNotification();
System.out.println("New message!");
}
}
}

@Override
publicvoid onDestroy() {
super.onDestroy();
System.out.println("Service:onDestroy");
}

}

三、在MainActivity.java中開啟和停止PollingService

[java]view plain
publicclass MainActivity extends Activity {

@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Start polling service
System.out.println("Start polling service...");
PollingUtils.startPollingService(this, 5, PollingService.class, PollingService.ACTION);
}

@Override
protectedvoid onDestroy() {
super.onDestroy();
//Stop polling service
System.out.println("Stop polling service...");
PollingUtils.stopPollingService(this, PollingService.class, PollingService.ACTION);
}

}

『貳』 android 如何使用Wake Lock來節電

android 使用Wake Lock來節電方法:
默認情況下,Android設備會在一段時間後使屏幕變暗,然後關閉屏幕顯示,最後停止CPU,有時用戶並不希望如此,因此Android提供了WakeLock類讓用戶實現自定義的電源管理,但是如果不合理使用這個功能,應用程序造成的電池電量消耗產生顯著的影響,所以建議當用戶觀看屏幕但是很少與屏幕進行交互時(如看視頻)使用,從而防止屏幕變暗。
如果一開始就對Android手機的硬體架構有一定的了解,設計出的應用程序通常不會成為待機電池殺手,而要設計出正確的通信機制與通信協議也並不困難。但如果不去了解而盲目設計,可就沒准了。
首先Android手機有兩個處理器,一個叫Application Processor(AP),一個叫Baseband Processor(BP)。AP是ARM架構的處理器,用於運行Linux+Android系統;BP用於運行實時操作系統(RTOS),通訊協議棧運行於BP的RTOS之上。非通話時間,BP的能耗基本上在5mA左右,而AP只要處於非休眠狀態,能耗至少在50mA以上,執行圖形運算時會更高。另外LCD工作時功耗在100mA左右,WIFI也在100mA左右。一般手機待機時,AP、LCD、WIFI均進入休眠狀態,這時Android中應用程序的代碼也會停止執行。
Android為了確保應用程序中關鍵代碼的正確執行,提供了Wake Lock的API,使得應用程序有許可權通過代碼阻止AP進入休眠狀態。但如果不領會Android設計者的意圖而濫用Wake Lock API,為了自身程序在後台的正常工作而長時間阻止AP進入休眠狀態,就會成為待機電池殺手。比如前段時間的某應用,比如現在仍然干著這事的某應用。

『叄』 android 電源管理休眠喚醒淺析

本文從源碼角度深度解析Android系統中WakeLock鎖的基本流程原理,對WakeLock使用、結構圖、acquire過程、PowerManagerService模塊、power.c文件中的實現細節以及wake_lock鎖的釋放流程進行了詳細闡述,並指出某些情況下WakeLock鎖可能會被disable的特殊場景。主要關注點包括:

1. **WakeLock使用**:WakeLock鎖有三種表現形式:PowerManger.WakeLock、PowerManagerService.WakeLock和SuspendBlocker。通過示例展示了如何在應用層申請WakeLock鎖。

2. **結構圖**:提供了WakeLock鎖的整體結構圖,幫助讀者理解各組成部分之間的關系。

3. **acquire過程**:解釋了創建newWakeLoc WakeLock對象的步驟,以及在特定條件下(如強制進入suspend狀態、進程不處於active狀態、DeviceIdle處於IDLE狀態且不在doze白名單中)WakeLock鎖可能被disable的情況。

4. **PowerManagerService**:重點關注PowerManagerService中的mWakeLockSuspendBlocker調用JNI方法nativeAcquireSuspendBlocker的過程。

5. **power.c文件中的實現**:闡述了power.c文件中的acquire_wake_lock實現,包括往指定文件節點寫入字元串數據的步驟(新版本為「/sys/power/wake_lock」,舊版本為「/sys/android_power/acquire_partial_wake_lock」)。

6. **release過程**:與acquire過程類似,釋放wake_lock鎖同樣遵循特定步驟,確保系統資源的合理管理。

本文旨在提供全面、深入的WakeLock鎖操作解析,為開發者在理解和應用這一功能時提供理論支撐和實踐指導。

熱點內容
javadebug 發布:2025-03-16 07:16:21 瀏覽:283
怎麼搭建linux伺服器ftp 發布:2025-03-16 07:07:38 瀏覽:988
晶元存儲原理 發布:2025-03-16 06:58:21 瀏覽:284
c語言中的整型 發布:2025-03-16 06:40:48 瀏覽:184
分部資料庫伺服器的IP地址有效 發布:2025-03-16 06:33:40 瀏覽:193
安卓項目如何配置tomacat 發布:2025-03-16 06:31:13 瀏覽:431
寫腳本測試 發布:2025-03-16 06:20:07 瀏覽:780
多個撥號寬頻如何配置 發布:2025-03-16 05:51:35 瀏覽:688
管理員c語言 發布:2025-03-16 05:40:17 瀏覽:342
安卓軟體上的圖案如何更改 發布:2025-03-16 05:35:57 瀏覽:748