編輯:Android 省電與效能提升
手機平放桌面時的兩張截圖,數值持續刷新。


Android 感測器的使用流程大致固定為四步:
1. 透過 getSystemService(Context.SENSOR_SERVICE) 取得 SensorManager;
2. 用 getDefaultSensor() 指定要監听的感測器型別;
3. 呼叫 registerListener() 註冊監聽器;
4. 在 onSensorChanged() 中取出數值,並在不需要時反注册以節省電量。
package com.example.kongqw.kqwsensorforaccelerometerdemo;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener {
private TextView mTvShow;
private SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvShow = (TextView) findViewById(R.id.tv_show);
// 取得感測器管理者
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// 取得加速度感測器
Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// 註冊監聽器,SENSOR_DELAY_UI 適合 UI 層級的更新頻率
mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
}
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
StringBuffer buffer = new StringBuffer();
buffer.append("X 方向的加速度為:").append(values[0]).append("\n");
buffer.append("Y 方向的加速度為:").append(values[1]).append("\n");
buffer.append("Z 方向的加速度為:").append(values[2]).append("\n");
mTvShow.setText(buffer);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onDestroy() {
super.onDestroy();
// 記得反注册,避免背景持續耗電
if (mSensorManager != null) {
mSensorManager.unregisterListener(this);
}
}
}
幾個實務上的重點:event.values 是一組長度為 3 的浮點陣列,分別對應 X、Y、Z 三軸,單位是 m/s²。手機平放時,Z 軸約為 9.8(重力方向),X、Y 則接近 0——這也是截圖中數值持續小幅跳動的原因,屬於感測器正常的雜訊。註冊時的更新頻率參數會直接影響耗電與回呼密度:SENSOR_DELAY_GAME 最快、SENSOR_DELAY_UI 適合一般 UI、SENSOR_DELAY_NORMAL 最省電。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://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=".MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="加速度感測器"
android:textSize="20dp" />
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/title"
android:textSize="18dp" />
</RelativeLayout>
傳感器專題(三):加速度傳感器與陀螺儀实战
本節引言 本節繼續拆 Android 傳感器,主角是加速度傳感器(Accelerometer sensor)與陀螺儀傳感器(Gyroscope sensor)。它們和方向
三星S4(i9500)省電入門:從螢幕與連線兩大耗電源著手
Galaxy S4 外型出眾、規格也夠,唯一常被詬病的就是續航。好消息是,這台手機的耗電來源相當集中——螢幕與無線連線幾乎佔了絕大部分。把這兩塊管好,續航就能明顯回來。
安卓省電應用:Snapdragon專用BatteryGuru
續航是智慧型手機的關鍵痛點。高通推出官方省電應用BatteryGuru,專為Snapdragon處理器設計,盼延長電池續航並優化效能。BatteryGuru會在安裝後2至
使用刷機精靈卸載定制機型預裝應用、加速手機系統
越來越多的用戶選擇安卓手機的同時,也發現手機廠商把自己的應用強行預裝到手機裡進行宣傳,尤其是在手機市場占了一席之地的合約機。由於內置應用大多無法直接刪除,大部分用戶不知道