English 中文(简体)
加速计应用
原标题:Accelerometer application

我必须做一个程序,通过摇动电话返回随机号码。 我用一个按钮做了它, 效果很好。 问题在于加速计。 它不起作用, 尽管我没有错误 。

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"

(笑声) (笑声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声)

最佳回答

It seems that you never add your class as an event listener. This code works for me:

@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)
{
}

也不错的主意是,在Resume/ on Stop/ 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提到的那样 清单中总是有你需要的许可

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签