[英]the accelerometer which detect the movement of the device that want to change the ringer mode of the phone to Ringer_Mode_silent
*我有一個嘗試使用加速度計的應用程序,該加速度計檢測想要將電話的振鈴模式更改為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);
}
}
使用加速度計可以輕松進行簡單的運動檢測(很難檢測模式),只需獲取sqrt(x ^ 2 + y ^ 2 + z ^ 2)-9.81)即可為您提供運動,其中x,y,z是加速度計的讀數。閾值,並在交叉時更改您的個人資料模式。
注意:
正如您在代碼加速度計中提到的那樣,不要給出方位角,滾動,俯仰。
values [0]:x軸上的加速度減去Gx
values [1]:y軸上的加速度減去Gy
values [2]:z軸上的加速度減去Gz
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.