2012-12-29 12 views
0

*我有一個應用程序試圖使用加速度計,該加速度計檢測設備的運動,想要更改鈴聲模式電話Ringer_Mode_silent *我意識到這項服務,檢測來電,但我不能理解如何改變手機的模式,當我移動設備,請我可以幫助我實現這個應用程序。加速度計,它檢測想要更改手機的鈴聲模式的設備的運動Ringer_Mode_silent

公共類MainActivity延伸活動實現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); 
} 
    } 

回答

0

簡單移動檢測很容易與加速計(圖案檢測是困難的)只取SQRT(X^2 + Y^2 + Z^2)-9.81),其給你的運動,其中x,y和z是加速度計讀數。與某些閾值比較,並在穿越時更改您的配置文件模式。

注:

,你在你的代碼的加速度計提到不給azimuthla,側傾,俯仰。

值[0]:加速減去的Gx在x軸

值[1]:加速減去GZ上Z-:在y軸

值[2]減去加速度Gy的軸

相關問題