*我有一个应用程序试图使用加速度计来检测想要将手机的振铃模式更改为 Ringer_Mode_silent 的设备的移动 *我实现了检测到来电的服务,但我不明白如何更改模式当我移动设备时手机在后台,请我帮我实现这个应用程序。
公共类 MainActivity 扩展 Activity 实现 SensorEventListener{
private SensorManager mSensorManager;
private Sensor mAccelerometre;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometre = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
Button b1=(Button)findViewById(R.id.start);
Button b2=(Button)findViewById(R.id.stop);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Intent intent=new Intent(Main.this,ServiceReceiver.class);
Intent serv = new Intent(MainActivity.this, ServiceBroadcast.class);
startService(serv);
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Intent intent=new Intent(Main.this,ServiceReceiver.class);
Intent serv = new Intent(MainActivity.this, ServiceBroadcast.class);
stopService(serv);
}
public void onSensorChanged()(SensorEvent event) {
float azimuth,pitch,roll;
if(mSensorManager==SensorManager.Sensor_Accelerometer)
{
azimuth = event.values[0];
pitch = event.values[1];
roll = event.values[2];
((TextView)findViewById(R.id.azimuth)).setText("Axe x "+azimuth);
((TextView)findViewById(R.id.pitch)).setText("Axe y "+pitch);
((TextView)findViewById(R.id.roll)).setText("Axe z "+roll);
}
}