0

我必须做一个通过摇晃手机返回随机数的应用程序。我用一个按钮做了它,它工作正常。问题出在加速度计上。它不起作用,即使我没有错误。

import java.util.Random;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RandomButtonActivity extends Activity implements SensorEventListener
{
    Button tasto1;

    TextView testo;
    TextView message;

    EditText limiteMin;
    EditText limiteMax;

    Random generator = new Random();

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        testo = (TextView) findViewById(R.id.textView1);

        limiteMin = (EditText) findViewById(R.id.editText2);
        limiteMax = (EditText) findViewById(R.id.EditText01);
    }

    public void onSensorChanged(SensorEvent event)
    {
        Sensor sensor = event.sensor;
        if (sensor.getType()==Sensor.TYPE_ACCELEROMETER)
        { 
        int j = Integer.parseInt(limiteMin.getText().toString());
        int i = Integer.parseInt(limiteMax.getText().toString())-j;
        int x = 0;

        if(i==0 && j==0)
        {
            x = generator.nextInt();

            while(x<0)
            {
                x = generator.nextInt();
            }
        }

        else 
        {
            if(j>=i)
            {               
                i = 0;
                j = 0;
            }
            else
            {
                x = generator.nextInt(i+1)+j;
                testo.setText(""+x);

            }
        }
        testo.setText(""+x);
        }

        else
        {

        }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) 
    {
    }
}

环顾互联网,这段代码应该可以正常工作。我没有对 XML 文件做任何事情。

编辑:

我补充说:

<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true"

到 Android:manifest 但没有任何改变。

4

1 回答 1

1

似乎您从未将您的类添加为事件侦听器。这段代码对我有用:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //This is what you are missing:
    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
}
public void onSensorChanged(SensorEvent event)
{
    Sensor sensor = event.sensor;
    if (sensor.getType() == Sensor.TYPE_ACCELEROMETER)
    {
        /// Do something
    }
}
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}

像这样覆盖 onResume/onStop/onPause 也是一个好主意

@Override protected void onResume() 
  {
    super.onResume();
    sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
  }

  @Override protected void onStop() 
  {
    sm.unregisterListener(this);
    super.onStop();
  }

正如 ernell 提到的,请始终记住在清单中拥有所需的权限。

于 2012-05-25T19:24:40.490 回答