如何在 Android 上使用传感器?

How to use Sensors on Android?

我是新手,完全不懂传感器的使用方法。我的目标只是暂时获取 Activity 上的旋转传感器数据。我会使用旋转矢量,但我看到 here 它们在 android 4.1.2 上不再可用...我目前的代码:

package thomas.drone;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements View.OnClickListener {
    public Button boutonPilote;
    TextView xminus=null;
    TextView yminus=null;
    TextView xplus=null;
    TextView yplus = null;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /*On déclare les capteurs que l'on veut utiliser*/
    SensorManager sMgr;
    sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
    Sensor motion;
    motion = sMgr.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR); //Ici un type Vecteur de rotation

    /**Permet de faire du Full screen sans activity spéciale*/
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

      /*On va relier les textes du layout aux valeurs de nos TextViews*/
    xminus = (TextView) findViewById(R.id.xminus);
    yminus = (TextView) findViewById(R.id.yminus);
    xplus = (TextView) findViewById(R.id.xplus);
    yplus = (TextView) findViewById(R.id.yplus);

    /*** On écoute le bouton Pilote/auto, qui switch d'une activité à l'autre**/
    boutonPilote = (Button) findViewById(R.id.bpilote);
    boutonPilote.setOnClickListener(this);

    /* et on active/enregistre le capteur*/
    sMgr.registerListener((android.hardware.SensorEventListener) this, motion,SensorManager.SENSOR_DELAY_NORMAL);


}

public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
public void onSensorChanged(SensorEvent event) {
    Toast.makeText(getApplicationContext(), "Ca marche !", Toast.LENGTH_LONG).show();
}

@Override
public void onClick(View v) {
    Intent i1 = new Intent(this, ModeAuto.class);
    this.startActivity(i1);
}
}

对不起评论,我是法国人(没有人是完美的:) 但是应用程序崩溃了...

这是 xml 文件,但我认为问题不在于此:`

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/xminus"
    android:layout_toStartOf="@+id/yplus"
    android:layout_marginRight="43dp"
    android:typeface="monospace"
    android:textSize="14sp"
    android:textColor="@color/blanc"
    android:text="@string/xmoins"
    android:layout_alignBaseline="@+id/xplus"
    android:layout_alignBottom="@+id/xplus"
    android:layout_toLeftOf="@+id/yplus" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:id="@+id/yminus"
    android:typeface="monospace"
    android:textSize="14sp"
    android:textColor="@color/blanc"
    android:text="@string/yminus"
    android:layout_alignBottom="@+id/bdecollage"
    android:layout_alignLeft="@+id/yplus"
    android:layout_alignStart="@+id/yplus" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:id="@+id/xplus"
    android:layout_toEndOf="@+id/yminus"
    android:typeface="monospace"
    android:textSize="14sp"
    android:textColor="@color/blanc"
    android:text="@string/xplus"
    android:layout_toStartOf="@+id/bleft"
    android:layout_alignTop="@+id/bleft"
    android:layout_toLeftOf="@+id/bleft" />

`

谢谢你,托马斯

这样做

sMgr.registerListener((android.hardware.SensorEventListener) this, ....

在很多情况下是非常危险的,它可能会导致您的崩溃。您必须实现接口 SensorEventListener,然后实现特定方法。

您的 Activity class 应该如下所示:

public class MainActivity extends Activity implements View.OnClickListener, SensorEventListener {

  // ...

  @Override
  public void onSensorChanged(SensorEvent event) {
   // ...
  }

  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {

  }
}

下一个问题是,你不知道@+id/bleft@id/bleft之间的区别。带加号的定义创建具有给定名称的新标识符。仅在 xml 文件中第一次出现时使用它。其他情况省略加号。