1.电量优化
电量优化首先我们要想到怎么去优化,要优化就要分析什么功能以及那些操作最耗电。
1.1 Battery Historian
Google提供了一个开源的电池历史数据分析工具 – Battery Historian。喜欢的同学可以去学习。Google在Udacity上发布了Android性能优化的在线课程。
1.1.1 安装
根据Battery Historian在github上的readme文件进行安装就行了。
1.1.2 使用
Battery Historian是Android 5.0开始引入的新API。通过下面的指令,可以得到设备上的电量消耗信息:
- //得到整个设备的电量消耗信息
- $ adb shell dumpsys batterystats > xxx.txt
- //得到指定app相关的电量消耗信息
- $ adb shell dumpsys batterystats > com.package.name > xxx.txt
得到原始电量消耗数据后,我们需要通过Google编写的一个python脚本把数据信息转换成可读性更好的html文件:
- $ python historian.py xxx.txt > xxx.html
打开这个转换过后的html文件,可以看到生成的列表数据,这里的数据信息量很大,这里就不展开了。根据数据显示我们可以看到那个地方的功能模块比较耗电,这样可以针对性的对部门代码进行电量优化。
2.Track Battery Status & Battery Manager
- //可以通过这段代码来获取手机的当前充电状态。
- // It is very easy to subscribe to changes to the battery state, but you can get the current
- // state by simply passing null in as your receiver. Nifty, isn't that?
- IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
- Intent batteryStatus = this.registerReceiver(null, filter);
- int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
- boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);
- if (acCharge) {
- Log.v(LOG_TAG, "The phone is charging!");
- }
上面描述了如何立即获取到手机的充电状态,得到充电状态信息之后,我们可以有针对性的对部分代码做优化。比如我们可以判断只有当前手机为AC充电状态时才去执行一些非常耗电的操作
- /**
- * This method checks for power by comparing the current battery state against all possible
- * plugged in states. In this case, a device may be considered plugged in either by USB, AC, or
- * wireless charge. (Wireless charge was introduced in API Level 17.)
- */
- private boolean checkForPower() {
- // It is very easy to subscribe to changes to the battery state, but you can get the current
- // state by simply passing null in as your receiver. Nifty, isn't that?
- IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
- Intent batteryStatus = this.registerReceiver(null, filter);
- // There are currently three ways a device can be plugged in. We should check them all.
- int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
- boolean usbCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_USB);
- boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);
- boolean wirelessCharge = false;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
- wirelessCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS);
- }
- return (usbCharge || acCharge || wirelessCharge);
- }
3.Wakelock
Android系统本身也会进行电量的优化,在系统和手机硬件交互过程中,比如系统进程的运行机制以及APP关闭回收机制,待机时屏幕会逐渐变暗直到关闭,硬件的CPU进入睡眠,这一切都是为了优化电量。但是即使在这种睡眠状态下,大多数应用还是会尝试进行工作,他们将不断的唤醒手机,比如我们的推送以及大家都会了解点的进程保活等等。
一个最简单的唤醒手机的方法是使用PowerManager.WakeLock的API来保持CPU工作并防止屏幕变暗关闭。这使得手机可以被唤醒,执行工作,然后回到睡眠状态。WakeLock还有android:keepScreenOn属性, 还可以让屏幕常量。知道如何获取WakeLock是简单的,可是及时释放WakeLock也是非常重要的,不恰当的使用WakeLock会导致严重错误。例如网络请求的数据返回时间不确定,导致本来只需要10s的事情一直等待了1个小时,这样会使得电量白白浪费了。这也是为何使用带超时参数的wakelock.acquice()方法是很关键的。
4.Network
Network就是各种网络状态下的请求以及数据传输。网络也涉及到网络的优化,具体会在写一篇网络优化中介绍如果做。总之,为了减少电量的消耗,最好做到批量执行网络请求,尽量避免频繁的间隔网络请求。可以减少电量的消耗也可以减少流量的消耗。
如何才能够把任务缓存起来,我们可以使用Android 5.0中的JobScheduler API。
5.Android 5.0中使用JobScheduler
JobScheduler API允许开发者在符合某些条件时创建执行在后台的任务。当你需要在稍后的某个时间点或当满足某个特定的条件时执行一个任务,例如当设备接通电源适配器或者连接到WIFI。在API 21 中google提供了JobScheduler 的xinAPI的组件来处理这样的场景。
JobScheduler在实际使用中,可以把不紧急的任务交给JobScheduler来处理,JobScheduler集中处理收到的任务,选择合适的时间合适的网络,然后再一起进行执行。
当一系列预置的条件被满足时,JobScheduler API为你的应用执行一个操作。与AlarmManager不同的是这个执行时间是不确定的。JobScheduler API允许同时执行多个任务。这允许你的应用执行某些指定的任务时不需要考虑时机控制引起的电池消耗。下面看代码示例:
1.创建JobSchedulerService类继承JobService,这个类必须实现两个方法onStartJob(JobParameters params)和 onStopJob(JobParameters params)。
- public class MyJobService extends JobService {
- private static final String LOG_TAG = "MyJobService";
- @Override public void onCreate() {
- super.onCreate();
- Log.i(LOG_TAG, "MyJobService created");
- }
- @Override public void onDestroy() {
- super.onDestroy();
- Log.i(LOG_TAG, "MyJobService destroyed");
- }
- @Override public boolean onStartJob(JobParameters params) {
- // This is where you would implement all of the logic for your job. Note that this runs
- // on the main thread, so you will want to use a separate thread for asynchronous work
- // (as we demonstrate below to establish a network connection).
- // If you use a separate thread, return true to indicate that you need a "reschedule" to
- // return to the job at some point in the future to finish processing the work. Otherwise,
- // return false when finished.
- Log.i(LOG_TAG, "Totally and completely working on job " + params.getJobId());
- // First, check the network, and then attempt to connect.
- if (isNetworkConnected()) {
- new SimpleDownloadTask().execute(params);
- return true;
- } else {
- Log.i(LOG_TAG, "No connection on job " + params.getJobId() + "; sad face");
- }
- return false;
- }
- @Override public boolean onStopJob(JobParameters params) {
- // Called if the job must be stopped before jobFinished() has been called. This may
- // happen if the requirements are no longer being met, such as the user no longer
- // connecting to WiFi, or the device no longer being idle. Use this callback to resolve
- // anything that may cause your application to misbehave from the job being halted.
- // Return true if the job should be rescheduled based on the retry criteria specified
- // when the job was created or return false to drop the job. Regardless of the value
- // returned, your job must stop executing.
- Log.i(LOG_TAG, "Whelp, something changed, so I'm calling it on job " + params.getJobId());
- return false;
- }
- /**
- * Determines if the device is currently online.
- */
- private boolean isNetworkConnected() {
- ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
- return (networkInfo != null && networkInfo.isConnected());
- }
- /**
- * Uses AsyncTask to create a task away from the main UI thread. This task creates a
- * HTTPUrlConnection, and then downloads the contents of the webpage as an InputStream.
- * The InputStream is then converted to a String, which is logged by the
- * onPostExecute() method.
- */
- private class SimpleDownloadTask extends AsyncTask < JobParameters,
- Void,
- String > {
- protected JobParameters mJobParam;
- @Override protected String doInBackground(JobParameters...params) {
- // cache system provided job requirements
- mJobParam = params[0];
- try {
- InputStream is = null;
- // Only display the first 50 characters of the retrieved web page content.
- int len = 50;
- URL url = new URL("https://www.google.com");
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setReadTimeout(10000); //10sec
- conn.setConnectTimeout(15000); //15sec
- conn.setRequestMethod("GET");
- //Starts the query
- conn.connect();
- int response = conn.getResponseCode();
- Log.d(LOG_TAG, "The response is: " + response);
- is = conn.getInputStream();
- // Convert the input stream to a string
- Reader reader = null;
- reader = new InputStreamReader(is, "UTF-8");
- char[] buffer = new char[len];
- reader.read(buffer);
- return new String(buffer);
- } catch(IOException e) {
- return "Unable to retrieve web page.";
- }
- }
- @Override protected void onPostExecute(String result) {
- jobFinished(mJobParam, false);
- Log.i(LOG_TAG, result);
- }
- }
- }
2.然后模拟通过点击Button触发N个任务,交给JobService来处理
- public class FreeTheWakelockActivity extends ActionBarActivity {
- public static final String LOG_TAG = "FreeTheWakelockActivity";
- TextView mWakeLockMsg;
- ComponentName mServiceComponent;
- @Override protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_wakelock);
- mWakeLockMsg = (TextView) findViewById(R.id.wakelock_txt);
- mServiceComponent = new ComponentName(this, MyJobService.class);
- Intent startServiceIntent = new Intent(this, MyJobService.class);
- startService(startServiceIntent);
- Button theButtonThatWakelocks = (Button) findViewById(R.id.wakelock_poll);
- theButtonThatWakelocks.setText(R.string.poll_server_button);
- theButtonThatWakelocks.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {
- pollServer();
- }
- });
- }
- /**
- * This method polls the server via the JobScheduler API. By scheduling the job with this API,
- * your app can be confident it will execute, but without the need for a wake lock. Rather, the
- * API will take your network jobs and execute them in batch to best take advantage of the
- * initial network connection cost.
- *
- * The JobScheduler API works through a background service. In this sample, we have
- * a simple service in MyJobService to get you started. The job is scheduled here in
- * the activity, but the job itself is executed in MyJobService in the startJob() method. For
- * example, to poll your server, you would create the network connection, send your GET
- * request, and then process the response all in MyJobService. This allows the JobScheduler API
- * to invoke your logic without needed to restart your activity.
- *
- * For brevity in the sample, we are scheduling the same job several times in quick succession,
- * but again, try to consider similar tasks occurring over time in your application that can
- * afford to wait and may benefit from batching.
- */
- public void pollServer() {
- JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
- for (int i = 0; i < 10; i++) {
- JobInfo jobInfo = new JobInfo.Builder(i, mServiceComponent).setMinimumLatency(3000) // 3 seconds
- .setOverrideDeadline(50000) // 50 seconds (for brevity in the sample)
- .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) // WiFi or data connections
- .build();
- mWakeLockMsg.append("Scheduling job " + i + "!\n");
- scheduler.schedule(jobInfo);
- }
- }
- }
Android 6.0新增了两个用来省电的技术Doze和App Standby.
Doze(睡眠):如果设备闲置了一段较长时间, Doze技术将通过延迟后台网络活动, CPU运行等来减少电量损耗。
App Standy(应用待机): 如果不是最近使用的APP, App Standy将延缓这个应用的后台网络活动。
来源: http://blog.csdn.net/haoaoo/article/details/78064638