通过handler结合Html刷新dialog界面的显示内容:
package com.pro.activity;
import java.util.List;
import com.pro.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
public class SensorCheckActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener,SensorEventListener{
private SharedPreferences sp;
private SensorManager sManager;
private Sensor sensor;
private Spanned msgMes;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.sensor);
sp = PreferenceManager.getDefaultSharedPreferences(this);
sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// sensor = sManager.getDefaultSensor(Sensor.TYPE_ALL);
}
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case Sensor.TYPE_ACCELEROMETER:
float[] data = (float[]) msg.obj;
// msgMes = "X轴方向:"+data[0]+"\nY周方向:"+data[1]+"\nZ轴方向:"+data[2];
msgMes = Html.fromHtml("
X轴方向加速度:"+data[0]+"
Y周方向加速度:"+data[1]+"
Z轴方向加速度:"+data[2]+"
");
textView.setText(msgMes);
break;
case Sensor.TYPE_LIGHT:
float[] light = (float[]) msg.obj;
msgMes = Html.fromHtml("
当前光线强度:"+light[0]+"
");
textView.setText(msgMes);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
float[] magnetic = (float[]) msg.obj;
msgMes = Html.fromHtml("
X轴方向磁感应:"+magnetic[0]+"
Y周方向磁感应:"+magnetic[1]+"
Z轴方向磁感应:"+magnetic[2]+"
");
textView.setText(msgMes);
break;
case Sensor.TYPE_PROXIMITY:
float[] proximity = (float[]) msg.obj;
msgMes = Html.fromHtml("
当前近距感应值:"+proximity[0]+"
");
textView.setText(msgMes);
break;
case Sensor.TYPE_ORIENTATION:
float[] orientation = (float[]) msg.obj;
msgMes = Html.fromHtml("
X轴方向:"+orientation[0]+"
Y周方向:"+orientation[1]+"
Z轴方向:"+orientation[2]+"
");
textView.setText(msgMes);
break;
case Sensor.TYPE_PRESSURE:
float[] pressure = (float[]) msg.obj;
msgMes = Html.fromHtml("
当前压力感应值:"+pressure[0]+"
");
textView.setText(msgMes);
break;
case Sensor.TYPE_TEMPERATURE:
float[] temperaure = (float[]) msg.obj;
msgMes = Html.fromHtml("
当前温度感应值:"+temperaure[0]+"
");
textView.setText(msgMes);
break;
default:
break;
}
};
};
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
sp.registerOnSharedPreferenceChangeListener(this);
// sManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
sp.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// sManager.unregisterListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
if("key_acceleration".equals(preference.getKey())){//加速度
showDialogView(this, Sensor.TYPE_ACCELEROMETER, (String) preference.getTitle());
}else if("key_ray".equals(preference.getKey())){//光线
showDialogView(this, Sensor.TYPE_LIGHT, (String) preference.getTitle());
}else if("key_dis".equals(preference.getKey())){//近距
showDialogView(this, Sensor.TYPE_PROXIMITY, (String) preference.getTitle());
}else if("key_temperature".equals(preference.getKey())){//温度
showDialogView(this, Sensor.TYPE_TEMPERATURE, (String) preference.getTitle());
}else if("key_way".equals(preference.getKey())){//方向
showDialogView(this, Sensor.TYPE_ORIENTATION, (String) preference.getTitle());
}else if ("key_magnetic".equals(preference.getKey())) {//磁场
showDialogView(this, Sensor.TYPE_MAGNETIC_FIELD, (String) preference.getTitle());
}else if("key_pressure".equals(preference.getKey())){//压力
showDialogView(this, Sensor.TYPE_PRESSURE, (String) preference.getTitle());
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
int type = event.sensor.getType();//.accuracy;
Message message = new Message();
switch (type) {
case Sensor.TYPE_ACCELEROMETER://SensorManager.SENSOR_ACCELEROMETER://加速度
float[] data = new float[3];
data[0] = event.values[0]; //x
data[1] = event.values[1]; //y
data[2] = event.values[2]; //z轴上的加速度
message.what = type;
message.obj = data;
handler.sendMessage(message);
break;
case Sensor.TYPE_ORIENTATION://SensorManager.SENSOR_ORIENTATION://方向
float[] orientation = new float[3];
orientation[0] = event.values[0]; //x
orientation[1] = event.values[1]; //y
orientation[2] = event.values[2]; //z轴上的方向
message.what = type;
message.obj = orientation;
handler.sendMessage(message);
break;
case Sensor.TYPE_PROXIMITY://SensorManager.SENSOR_PROXIMITY://距离
float[] proximity = new float[1];
proximity[0] = event.values[0]; //
message.what = type;
message.obj = proximity;
handler.sendMessage(message);
break;
case Sensor.TYPE_PRESSURE://Sensor.TYPE_PRESSURE://压力
float[] pressure = new float[1];
pressure[0] = event.values[0]; //
message.what = type;
message.obj = pressure;
handler.sendMessage(message);
break;
case Sensor.TYPE_LIGHT://SensorManager.SENSOR_LIGHT://光线
float[] light = new float[1];
light[0] = event.values[0]; //
message.what = type;
message.obj = light;
handler.sendMessage(message);
break;
case Sensor.TYPE_TEMPERATURE://SensorManager.SENSOR_TEMPERATURE://温度
float[] temperature = new float[1];
temperature[0] = event.values[0]; //
message.what = type;
message.obj = temperature;
handler.sendMessage(message);
break;
case Sensor.TYPE_MAGNETIC_FIELD://磁场
float[] magnetic = new float[3];
magnetic[0] = event.values[0]; //x
magnetic[1] = event.values[1]; //y
magnetic[2] = event.values[2]; //z轴上的磁感应
message.what = type;
message.obj = magnetic;
handler.sendMessage(message);
break;
default:
break;
}
}
/**
* 显示不同感应器的测试视图
*/
private void showDialogView(Context ctx,int sensorType,String msgTitle){
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
if(sManager != null){
// sensor = sManager.getDefaultSensor(Sensor.TYPE_ALL);
sensor = sManager.getDefaultSensor(sensorType);
if(sensor != null){
sManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}else{//手机不支持此类感应器类型
msgMes = Html.fromHtml("
该手机不支持此类感应器类型");
builder.setMessage(msgMes);
}
}
textView = new TextView(ctx);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(lp);
builder.setTitle(msgTitle);
builder.setIcon(R.drawable.icon);
// builder.setMessage(msgMes);
builder.setView(textView);
builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sManager.unregisterListener(SensorCheckActivity.this);
}
});
builder.show()
.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
textView.setText(msgMes);
}
});
}
}