自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android性能優(yōu)化之電量篇

移動開發(fā) Android
Google近期在Udacity上發(fā)布了Android性能優(yōu)化的在線課程,分別從渲染,運算與內(nèi)存,電量幾個方面介紹了如何去優(yōu)化性能,這些課程是Google之前在Youtube上發(fā)布的Android性能優(yōu)化典范專題課程的細(xì)化與補(bǔ)充。

Google近期在Udacity上發(fā)布了Android性能優(yōu)化的在線課程,分別從渲染,運算與內(nèi)存,電量幾個方面介紹了如何去優(yōu)化性能,這些課程是Google之前在Youtube上發(fā)布的Android性能優(yōu)化典范專題課程的細(xì)化與補(bǔ)充。

下面是電量篇章的學(xué)習(xí)筆記,部分內(nèi)容與前面的性能優(yōu)化典范有重合,歡迎大家一起學(xué)習(xí)交流!
1)Understanding Battery Drain

手機(jī)各個硬件模塊的耗電量是不一樣的,有些模塊非常耗電,而有些模塊則相對顯得耗電量小很多。

 

電量消耗的計算與統(tǒng)計是一件麻煩而且矛盾的事情,記錄電量消耗本身也是一個費電量的事情。唯一可行的方案是使用第三方監(jiān)測電量的設(shè)備,這樣才能夠獲取到真實的電量消耗。

當(dāng)設(shè)備處于待機(jī)狀態(tài)時消耗的電量是極少的,以N5為例,打開飛行模式,可以待機(jī)接近1個月??墒屈c亮屏幕,硬件各個模塊就需要開始工作,這會需要消耗很多電量。

使用WakeLock或者JobScheduler喚醒設(shè)備處理定時的任務(wù)之后,一定要及時讓設(shè)備回到初始狀態(tài)。每次喚醒蜂窩信號進(jìn)行數(shù)據(jù)傳遞,都會消耗很多電量,它比WiFi等操作更加的耗電。


2)Battery Historian

Battery Historian是Android 5.0開始引入的新API。通過下面的指令,可以得到設(shè)備上的電量消耗信息:

 

 

  1. $ adb shell dumpsys batterystats > xxx.txt //得到整個設(shè)備的電量消耗信息 
  2. $ adb shell dumpsys batterystats > com.package.name > xxx.txt //得到指定app相關(guān)的電量消耗信息 

得到了原始的電量消耗數(shù)據(jù)之后,我們需要通過Google編寫的一個python腳本把數(shù)據(jù)信息轉(zhuǎn)換成可讀性更好的html文件:

  1. $ python historian.py xxx.txt > xxx.html 

打開這個轉(zhuǎn)換過后的html文件,可以看到類似TraceView生成的列表數(shù)據(jù),這里的數(shù)據(jù)信息量很大,這里就不展開了。


3)Track Battery Status & Battery Manager

我們可以通過下面的代碼來獲取手機(jī)的當(dāng)前充電狀態(tài):

  1. // It is very easy to subscribe to changes to the battery state, but you can get the current 
  2. // state by simply passing null in as your receiver. Nifty, isn't that? 
  3. IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); 
  4. Intent batteryStatus = this.registerReceiver(null, filter); 
  5. int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); 
  6. boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC); 
  7. if (acCharge) { 
  8. Log.v(LOG_TAG,“The phone is charging!”); 

在上面的例子演示了如何立即獲取到手機(jī)的充電狀態(tài),得到充電狀態(tài)信息之后,我們可以有針對性的對部分代碼做優(yōu)化。比如我們可以判斷只有當(dāng)前手機(jī)為AC充電狀態(tài)時 才去執(zhí)行一些非常耗電的操作。

  1. /** 
  2. * This method checks for power by comparing the current battery state against all possible 
  3. * plugged in states. In this case, a device may be considered plugged in either by USB, AC, or 
  4. * wireless charge. (Wireless charge was introduced in API Level 17.) 
  5. */ 
  6. private boolean checkForPower() { 
  7. // It is very easy to subscribe to changes to the battery state, but you can get the current 
  8. // state by simply passing null in as your receiver. Nifty, isn't that? 
  9. IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); 
  10. Intent batteryStatus = this.registerReceiver(null, filter); 
  11.  
  12. // There are currently three ways a device can be plugged in. We should check them all. 
  13. int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); 
  14. boolean usbCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_USB); 
  15. boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC); 
  16. boolean wirelessCharge = false
  17. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
  18. wirelessCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS); 
  19. return (usbCharge || acCharge || wirelessCharge); 

4)Wakelock and Battery Drain

高效的保留更多的電量與不斷促使用戶使用你的App會消耗電量,這是矛盾的選擇題。不過我們可以使用一些更好的辦法來平衡兩者。

假設(shè)你的手機(jī)里面裝了大量的社交類應(yīng)用,即使手機(jī)處于待機(jī)狀態(tài),也會經(jīng)常被這些應(yīng)用喚醒用來檢查同步新的數(shù)據(jù)信息。Android會不斷關(guān)閉各種硬件來延長手機(jī)的待機(jī)時間,首先屏幕會逐漸變暗直至關(guān)閉,然后CPU進(jìn)入睡眠,這一切操作都是為了節(jié)約寶貴的電量資源。但是即使在這種睡眠狀態(tài)下,大多數(shù)應(yīng)用還是會嘗試進(jìn)行工作,他們將不斷的喚醒手機(jī)。一個最簡單的喚醒手機(jī)的方法是使用PowerManager.WakeLock的API來保持CPU工作并防止屏幕變暗關(guān)閉。這使得手機(jī)可以被喚醒,執(zhí)行工作,然后回到睡眠狀態(tài)。知道如何獲取WakeLock是簡單的,可是及時釋放WakeLock也是非常重要的,不恰當(dāng)?shù)氖褂肳akeLock會導(dǎo)致嚴(yán)重錯誤。例如網(wǎng)絡(luò)請求的數(shù)據(jù)返回時間不確定,導(dǎo)致本來只需要10s的事情一直等待了1個小時,這樣會使得電量白白浪費了。這也是為何使用帶超時參數(shù)的wakelock.acquice()方法是很關(guān)鍵的。

但是僅僅設(shè)置超時并不足夠解決問題,例如設(shè)置多長的超時比較合適?什么時候進(jìn)行重試等等?解決上面的問題,正確的方式可能是使用非精準(zhǔn)定時器。通常情況下,我們會設(shè)定一個時間進(jìn)行某個操作,但是動態(tài)修改這個時間也許會更好。例如,如果有另外一個程序需要比你設(shè)定的時間晚5分鐘喚醒,***能夠等到那個時候,兩個任務(wù)捆綁一起同時進(jìn)行,這就是非精確定時器的核心工作原理。我們可以定制計劃的任務(wù),可是系統(tǒng)如果檢測到一個更好的時間,它可以推遲你的任務(wù),以節(jié)省電量消耗。

 

 

這正是JobScheduler API所做的事情。它會根據(jù)當(dāng)前的情況與任務(wù),組合出理想的喚醒時間,例如等到正在充電或者連接到WiFi的時候,或者集中任務(wù)一起執(zhí)行。我們可以通過這個API實現(xiàn)很多免費的調(diào)度算法。
5)Network and Battery Drain

下面內(nèi)容來自官方Training文檔中高效下載章節(jié)關(guān)于手機(jī)(Radio)蜂窩信號對電量消耗的介紹。

通常情況下,使用3G移動網(wǎng)絡(luò)傳輸數(shù)據(jù),電量的消耗有三種狀態(tài):

Full power: 能量***的狀態(tài),移動網(wǎng)絡(luò)連接被激活,允許設(shè)備以***的傳輸速率進(jìn)行操作。
Low power: 一種中間狀態(tài),對電量的消耗差不多是Full power狀態(tài)下的50%。
Standby: ***的狀態(tài),沒有數(shù)據(jù)連接需要傳輸,電量消耗最少。

下圖是一個典型的3G Radio State Machine的圖示

 

總之,為了減少電量的消耗,在蜂窩移動網(wǎng)絡(luò)下,***做到批量執(zhí)行網(wǎng)絡(luò)請求,盡量避免頻繁的間隔網(wǎng)絡(luò)請求。

通過前面學(xué)習(xí)到的Battery Historian我們可以得到設(shè)備的電量消耗數(shù)據(jù),如果數(shù)據(jù)中的移動蜂窩網(wǎng)絡(luò)(Mobile Radio)電量消耗呈現(xiàn)下面的情況,間隔很小,又頻繁斷斷續(xù)續(xù)的出現(xiàn),說明電量消耗性能很不好:

經(jīng)過優(yōu)化之后,如果呈現(xiàn)下面的圖示,說明電量消耗的性能是良好的:

 

另外WiFi連接下,網(wǎng)絡(luò)傳輸?shù)碾娏肯囊纫苿泳W(wǎng)絡(luò)少很多,應(yīng)該盡量減少移動網(wǎng)絡(luò)下的數(shù)據(jù)傳輸,多在WiFi環(huán)境下傳輸數(shù)據(jù)。

 

那么如何才能夠把任務(wù)緩存起來,做到批量化執(zhí)行呢?下面就輪到Job Scheduler出場了。
6)Using Job Scheduler

使用Job Scheduler,應(yīng)用需要做的事情就是判斷哪些任務(wù)是不緊急的,可以交給Job Scheduler來處理,Job Scheduler集中處理收到的任務(wù),選擇合適的時間,合適的網(wǎng)絡(luò),再一起進(jìn)行執(zhí)行。

下面是使用Job Scheduler的一段簡要示例,需要先有一個JobService:

 

  1. public class MyJobService extends JobService { 
  2. private static final String LOG_TAG = "MyJobService"
  3.  
  4. @Override 
  5. public void onCreate() { 
  6. super.onCreate(); 
  7. Log.i(LOG_TAG, "MyJobService created"); 
  8.  
  9. @Override 
  10. public void onDestroy() { 
  11. super.onDestroy(); 
  12. Log.i(LOG_TAG, "MyJobService destroyed"); 
  13.  
  14. @Override 
  15. public boolean onStartJob(JobParameters params) { 
  16. // This is where you would implement all of the logic for your job. Note that this runs 
  17. // on the main thread, so you will want to use a separate thread for asynchronous work 
  18. // (as we demonstrate below to establish a network connection). 
  19. // If you use a separate thread, return true to indicate that you need a "reschedule" to 
  20. // return to the job at some point in the future to finish processing the work. Otherwise, 
  21. // return false when finished. 
  22. Log.i(LOG_TAG, "Totally and completely working on job " + params.getJobId()); 
  23. // First, check the network, and then attempt to connect. 
  24. if (isNetworkConnected()) { 
  25. new SimpleDownloadTask() .execute(params); 
  26. return true
  27. else { 
  28. Log.i(LOG_TAG, "No connection on job " + params.getJobId() + "; sad face"); 
  29. return false
  30.  
  31. @Override 
  32. public boolean onStopJob(JobParameters params) { 
  33. // Called if the job must be stopped before jobFinished() has been called. This may 
  34. // happen if the requirements are no longer being met, such as the user no longer 
  35. // connecting to WiFi, or the device no longer being idle. Use this callback to resolve 
  36. // anything that may cause your application to misbehave from the job being halted. 
  37. // Return true if the job should be rescheduled based on the retry criteria specified 
  38. // when the job was created or return false to drop the job. Regardless of the value 
  39. // returned, your job must stop executing. 
  40. Log.i(LOG_TAG, "Whelp, something changed, so I'm calling it on job " + params.getJobId()); 
  41. return false
  42.  
  43. /** 
  44. * Determines if the device is currently online. 
  45. */ 
  46. private boolean isNetworkConnected() { 
  47. ConnectivityManager connectivityManager = 
  48. (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
  49. NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 
  50. return (networkInfo != null && networkInfo.isConnected()); 
  51.  
  52. /** 
  53. * Uses AsyncTask to create a task away from the main UI thread. This task creates a 
  54. * HTTPUrlConnection, and then downloads the contents of the webpage as an InputStream. 
  55. * The InputStream is then converted to a String, which is logged by the 
  56. * onPostExecute() method. 
  57. */ 
  58. private class SimpleDownloadTask extends AsyncTask<JobParameters, Void, String> { 
  59.  
  60. protected JobParameters mJobParam; 
  61.  
  62. @Override 
  63. protected String doInBackground(JobParameters... params) { 
  64. // cache system provided job requirements 
  65. mJobParam = params[0]; 
  66. try { 
  67. InputStream is = null
  68. // Only display the first 50 characters of the retrieved web page content. 
  69. int len = 50
  70.  
  71. URL url = new URL("https://www.google.com"); 
  72. HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
  73. conn.setReadTimeout(10000); //10sec 
  74. conn.setConnectTimeout(15000); //15sec 
  75. conn.setRequestMethod("GET"); 
  76. //Starts the query 
  77. conn.connect(); 
  78. int response = conn.getResponseCode(); 
  79. Log.d(LOG_TAG, "The response is: " + response); 
  80. is = conn.getInputStream(); 
  81.  
  82. // Convert the input stream to a string 
  83. Reader reader = null
  84. reader = new InputStreamReader(is, "UTF-8"); 
  85. char[] buffer = new char[len]; 
  86. reader.read(buffer); 
  87. return new String(buffer); 
  88.  
  89. catch (IOException e) { 
  90. return "Unable to retrieve web page."
  91.  
  92. @Override 
  93. protected void onPostExecute(String result) { 
  94. jobFinished(mJobParam, false); 
  95. Log.i(LOG_TAG, result); 

 

然后模擬通過點擊Button觸發(fā)N個任務(wù),交給JobService來處

 

  1. public class FreeTheWakelockActivity extends ActionBarActivity { 
  2. public static final String LOG_TAG = "FreeTheWakelockActivity"
  3.  
  4. TextView mWakeLockMsg; 
  5. ComponentName mServiceComponent; 
  6.  
  7. @Override 
  8. protected void onCreate(Bundle savedInstanceState) { 
  9. super.onCreate(savedInstanceState); 
  10. setContentView(R.layout.activity_wakelock); 
  11.  
  12. mWakeLockMsg = (TextView) findViewById(R.id.wakelock_txt); 
  13. mServiceComponent = new ComponentName(this, MyJobService.class); 
  14. Intent startServiceIntent = new Intent(this, MyJobService.class); 
  15. startService(startServiceIntent); 
  16.  
  17. Button theButtonThatWakelocks = (Button) findViewById(R.id.wakelock_poll); 
  18. theButtonThatWakelocks.setText(R.string.poll_server_button); 
  19.  
  20. theButtonThatWakelocks.setOnClickListener(new View.OnClickListener() { 
  21. @Override 
  22. public void onClick(View v) { 
  23. pollServer(); 
  24. }); 
  25.  
  26. /** 
  27. * This method polls the server via the JobScheduler API. By scheduling the job with this API, 
  28. * your app can be confident it will execute, but without the need for a wake lock. Rather, the 
  29. * API will take your network jobs and execute them in batch to best take advantage of the 
  30. * initial network connection cost. 
  31. * 
  32. * The JobScheduler API works through a background service. In this sample, we have 
  33. * a simple service in MyJobService to get you started. The job is scheduled here in 
  34. * the activity, but the job itself is executed in MyJobService in the startJob() method. For 
  35. * example, to poll your server, you would create the network connection, send your GET 
  36. * request, and then process the response all in MyJobService. This allows the JobScheduler API 
  37. * to invoke your logic without needed to restart your activity. 
  38. * 
  39. * For brevity in the sample, we are scheduling the same job several times in quick succession, 
  40. * but again, try to consider similar tasks occurring over time in your application that can 
  41. * afford to wait and may benefit from batching. 
  42. */ 
  43. public void pollServer() { 
  44. JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); 
  45. for (int i=0; i<10; i++) { 
  46. JobInfo jobInfo = new JobInfo.Builder(i, mServiceComponent) 
  47. .setMinimumLatency(5000// 5 seconds 
  48. .setOverrideDeadline(60000// 60 seconds (for brevity in the sample) 
  49. .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) // WiFi or data connections 
  50. .build(); 
  51.  
  52. mWakeLockMsg.append("Scheduling job " + i + "!\n"); 
  53. scheduler.schedule(jobInfo); 

 

責(zé)任編輯:chenqingxiang 來源: 胡凱的博客
相關(guān)推薦

2015-09-16 14:37:50

Android性能優(yōu)化運算

2015-09-16 13:54:30

Android性能優(yōu)化渲染

2021-07-29 14:20:34

網(wǎng)絡(luò)優(yōu)化移動互聯(lián)網(wǎng)數(shù)據(jù)存儲

2015-09-16 15:21:23

Android性能優(yōu)化內(nèi)存

2013-02-20 14:32:37

Android開發(fā)性能

2019-12-13 10:25:08

Android性能優(yōu)化啟動優(yōu)化

2009-08-13 16:22:18

ASP.NET性能優(yōu)化

2020-06-11 13:03:04

性能優(yōu)化緩存

2009-08-13 15:49:18

ASP.NET性能優(yōu)化

2013-09-17 10:32:08

Android性能優(yōu)化數(shù)據(jù)庫

2017-01-15 15:13:37

Android性能優(yōu)化優(yōu)化點

2019-09-25 08:03:21

Android加速Google

2017-12-23 14:38:41

Android編程開發(fā)優(yōu)化

2021-07-05 14:55:28

前端優(yōu)化圖片

2019-03-15 15:00:49

Webpack構(gòu)建速度前端

2024-02-20 19:53:57

網(wǎng)絡(luò)通信協(xié)議

2012-06-20 13:54:44

架構(gòu)性能優(yōu)化

2022-02-16 14:10:51

服務(wù)器性能優(yōu)化Linux

2021-11-29 11:13:45

服務(wù)器網(wǎng)絡(luò)性能

2021-07-27 20:51:02

AndroidDNS網(wǎng)絡(luò)
點贊
收藏

51CTO技術(shù)棧公眾號