我创建了一个简单的应用程序来使用加速度计,它的作用是什么,当手机倾斜时,即在x轴上播放声音..我已成功完成程序并将其部署在我的手机中(无错误) )但该程序正在工作但是没有播放倾斜声音,即加速度计不起作用..我认为我的手机有sm问题,它的传感器工作时完美地玩游戏,浏览器和所有......问题是什么?请问有谁说什么是问题?这是我手机的问题吗?我也附上了代码..
package com.antony.am;
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.MediaPlayer;
import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity implements SensorEventListener{
/** Called when the activity is first created. */
float x,y,sensorX,sensorY;
TextView tv1,tv2,tv3;
int i=0;
SensorManager sm;
MediaPlayer mp;
MyEventListener el;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
el=new MyEventListener();
x=y=sensorX=sensorY=0;
// tv1=(TextView) findViewById(R.id.textView1);
// tv2=(TextView) findViewById(R.id.textView2);
mp=MediaPlayer.create(this, R.raw.jingle);
sm=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){
Sensor s=sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(el, s, SensorManager.SENSOR_DELAY_FASTEST);
}
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent e) {
//e.values[0]=x direction change
//e.values[1]=y direction change
//e.values[2]=z direction change
if((int)e.values[0]==6){
mp.start();
}
else if((int)e.values[0]==-6){
mp.start();
}
//tv1.setTextSize(e.values[0]*10);
//tv2.setTextSize(e.values[0]*10);
//tv1.setText(Integer.toString((int) e.values[0]));
//tv2.setText(Integer.toString((int) e.values[1]));
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
sm.unregisterListener(el);
super.onPause();
mp.stop();
}
class MyEventListener implements SensorEventListener{
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
}
}
}