悦知文件出版的《Android SDK 开发范例大全》上有一个例子,就是当手机反过来时自动静音,这里面涉及到传感器的编程,今天练手,输入了一下,发现需要完善几个问题(书上只是点到为止嘛,更多的工作需要自己再努力 :-) ):
1.如果旋转方向为X方向也应该自动静音
2.如果已设置了静音或普通模式则不需要再重复设置
3.在模拟器上有可能检测不到传感器,所以需要判断处理
下面贴出我完善了一下的代码,当然还有待改进(已重构了部分代码) :-)
package com.ad.android.reversemute;
import java.util.List;
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.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
private SensorManager mSensorManager;
private boolean mRegisteredSensor;
private TextView mtvStatus;
private int intCurRingerMode;
private final String TAG="ReverseMute";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mtvStatus=(TextView)findViewById(R.id.tvStatus);
mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
//取得现在的AudioManager模式显示在TextView中
intCurRingerMode=GetAudioManagerMode();
SetStatus(intCurRingerMode);
}
private void SetStatus(String info)
{
mtvStatus.setText(info);
}
private void SetStatus(int mode)
{
switch(mode)
{
//正常模式
case AudioManager.RINGER_MODE_NORMAL:
mtvStatus.setText(R.string.str_normal_mode);
break;
case AudioManager.RINGER_MODE_SILENT:
mtvStatus.setText(R.string.str_silent_mode);
break;
case AudioManager.RINGER_MODE_VIBRATE:
mtvStatus.setText(R.string.str_vibrate_mode);
break;
}
}
//取得当前的AudioManager模式
private int GetAudioManagerMode()
{
int r=-1;
try
{
AudioManager audioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
if(audioManager!=null)
{
r=audioManager.getRingerMode();
}
}
catch(Exception e)
{
mtvStatus.setText(e.toString());
e.printStackTrace();
}
return r;
}
//创建SensorListener捕捉onSensorChanged事件
private final SensorEventListener mSensorListener=new SensorEventListener()
{
@Override
public void onAccuracyChanged(Sensor sensor,int accuracy)
{
//TODO
}
@Override
public void onSensorChanged(SensorEvent event)
{
if(event.sensor.getType()==Sensor.TYPE_ORIENTATION)
{
//取得平面左右倾斜的Pitch角度
float x=event.values[SensorManager.DATA_X];
float y=event.values[SensorManager.DATA_Y];
//float z=event.values[SensorManager.DATA_Z];
//正面向下(Y轴旋转),经实验结果为小于-120为翻背面
//同样,增加对X轴旋转的判断
if(y<-120|x<-120)
{
//如果已经设置,则不再重复设置
if(intCurRingerMode==AudioManager.RINGER_MODE_NORMAL)
{
//先设置为静音模式
SetRingMode(AudioManager.RINGER_MODE_SILENT);
//再设置为振动模式
SetRingMode(AudioManager.RINGER_MODE_VIBRATE);
intCurRingerMode=AudioManager.RINGER_MODE_SILENT;
}
Log.i(TAG,"正面朝下");
}
else //正面朝上
{
if(intCurRingerMode==AudioManager.RINGER_MODE_SILENT)
{
SetRingMode(AudioManager.RINGER_MODE_NORMAL);
intCurRingerMode=AudioManager.RINGER_MODE_NORMAL;
}
Log.i(TAG,"正面朝上");
}
}
}
private void SetRingMode(int mode)
{
try
{
AudioManager audioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
if(audioManager!=null)
{
audioManager.setRingerMode(mode);
}
SetStatus(mode);
}
catch(Exception e)
{
mtvStatus.setText(e.toString());
e.printStackTrace();
}
}
};
@Override
protected void onPause() {
// TODO Auto-generated method stub
if(mRegisteredSensor)
{
mSensorManager.unregisterListener(mSensorListener);
}
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
List<Sensor> sensors=mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
try
{
//在模拟器上可能找不到传感器,所以如果不判断sensors的size的话在模拟器上调试会引发异常
if(sensors.size()>=1)
{
mRegisteredSensor=mSensorManager.registerListener(mSensorListener,sensors.get(0),SensorManager.SENSOR_DELAY_NORMAL);
}
else
{
mtvStatus.setText("没有找到传感器");
}
}
catch(Exception e)
{
mtvStatus.setText(e.toString());
e.printStackTrace();
}
super.onResume();
}
import java.util.List;
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.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
private SensorManager mSensorManager;
private boolean mRegisteredSensor;
private TextView mtvStatus;
private int intCurRingerMode;
private final String TAG="ReverseMute";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mtvStatus=(TextView)findViewById(R.id.tvStatus);
mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
//取得现在的AudioManager模式显示在TextView中
intCurRingerMode=GetAudioManagerMode();
SetStatus(intCurRingerMode);
}
private void SetStatus(String info)
{
mtvStatus.setText(info);
}
private void SetStatus(int mode)
{
switch(mode)
{
//正常模式
case AudioManager.RINGER_MODE_NORMAL:
mtvStatus.setText(R.string.str_normal_mode);
break;
case AudioManager.RINGER_MODE_SILENT:
mtvStatus.setText(R.string.str_silent_mode);
break;
case AudioManager.RINGER_MODE_VIBRATE:
mtvStatus.setText(R.string.str_vibrate_mode);
break;
}
}
//取得当前的AudioManager模式
private int GetAudioManagerMode()
{
int r=-1;
try
{
AudioManager audioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
if(audioManager!=null)
{
r=audioManager.getRingerMode();
}
}
catch(Exception e)
{
mtvStatus.setText(e.toString());
e.printStackTrace();
}
return r;
}
//创建SensorListener捕捉onSensorChanged事件
private final SensorEventListener mSensorListener=new SensorEventListener()
{
@Override
public void onAccuracyChanged(Sensor sensor,int accuracy)
{
//TODO
}
@Override
public void onSensorChanged(SensorEvent event)
{
if(event.sensor.getType()==Sensor.TYPE_ORIENTATION)
{
//取得平面左右倾斜的Pitch角度
float x=event.values[SensorManager.DATA_X];
float y=event.values[SensorManager.DATA_Y];
//float z=event.values[SensorManager.DATA_Z];
//正面向下(Y轴旋转),经实验结果为小于-120为翻背面
//同样,增加对X轴旋转的判断
if(y<-120|x<-120)
{
//如果已经设置,则不再重复设置
if(intCurRingerMode==AudioManager.RINGER_MODE_NORMAL)
{
//先设置为静音模式
SetRingMode(AudioManager.RINGER_MODE_SILENT);
//再设置为振动模式
SetRingMode(AudioManager.RINGER_MODE_VIBRATE);
intCurRingerMode=AudioManager.RINGER_MODE_SILENT;
}
Log.i(TAG,"正面朝下");
}
else //正面朝上
{
if(intCurRingerMode==AudioManager.RINGER_MODE_SILENT)
{
SetRingMode(AudioManager.RINGER_MODE_NORMAL);
intCurRingerMode=AudioManager.RINGER_MODE_NORMAL;
}
Log.i(TAG,"正面朝上");
}
}
}
private void SetRingMode(int mode)
{
try
{
AudioManager audioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
if(audioManager!=null)
{
audioManager.setRingerMode(mode);
}
SetStatus(mode);
}
catch(Exception e)
{
mtvStatus.setText(e.toString());
e.printStackTrace();
}
}
};
@Override
protected void onPause() {
// TODO Auto-generated method stub
if(mRegisteredSensor)
{
mSensorManager.unregisterListener(mSensorListener);
}
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
List<Sensor> sensors=mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
try
{
//在模拟器上可能找不到传感器,所以如果不判断sensors的size的话在模拟器上调试会引发异常
if(sensors.size()>=1)
{
mRegisteredSensor=mSensorManager.registerListener(mSensorListener,sensors.get(0),SensorManager.SENSOR_DELAY_NORMAL);
}
else
{
mtvStatus.setText("没有找到传感器");
}
}
catch(Exception e)
{
mtvStatus.setText(e.toString());
e.printStackTrace();
}
super.onResume();
}
}