震动传感器不工作

Posted

技术标签:

【中文标题】震动传感器不工作【英文标题】:Shake sensor is not working 【发布时间】:2014-01-23 21:45:30 【问题描述】:

我希望我的震动传感器能够工作。在摇动我的安卓手机以使某些事情发生时,现在当我摇动我的手机时没有任何反应。我的代码如下:

private SensorManager mSensorManager;
    private float mAccel; // acceleration apart from gravity
    private float mAccelCurrent; // current acceleration including gravity
    private float mAccelLast; // last acceleration including gravity

和创建:

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
        mAccel = 0.00f;
        mAccelCurrent = SensorManager.GRAVITY_EARTH;
        mAccelLast = SensorManager.GRAVITY_EARTH;

关于活动

private final SensorEventListener mSensorListener = new SensorEventListener() 

        public void onSensorChanged(SensorEvent se) 
          float x = se.values[0];
          float y = se.values[1];
          float z = se.values[2];
          mAccelLast = mAccelCurrent;
          mAccelCurrent = FloatMath.sqrt(x*x + y*y + z*z);
          float delta = mAccelCurrent - mAccelLast;
          mAccel = mAccel * 0.9f + delta * 0.1f; // perform low-cut filter
        

        public void onAccuracyChanged(Sensor sensor, int accuracy) 
            if(mAccel > 0.1)
            
             Toast toast = Toast.makeText(getApplicationContext(), "Device has shaken.", Toast.LENGTH_LONG); toast.show(); 
            

        
   ;

【问题讨论】:

【参考方案1】:

嗨,我在这里添加了震动传感器的示例源代码。请试试这个例子。希望它对您有所帮助。如果您有任何问题,请告诉我。谢谢。

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:layout_above="@+id/footer"
    android:padding="10dp"
    android:background="#eaeae8" >

    <TextView
        android:id="@+id/txtSettings"
        android:layout_
        android:layout_ 
        android:padding="10dp"
        android:text="Enable and Disable the Sensor">

    </TextView>

    <TextView 
            android:id="@+id/label"
        android:layout_
        android:layout_
        android:padding="10dip"
        android:textSize="16sp"
        android:textStyle="bold" >
        </TextView>

        <RadioGroup
            android:id="@+id/sensorSettings"
            android:layout_
            android:layout_
            android:layout_alignLeft="@+id/txtSettings"
            android:layout_below="@+id/txtSettings"
            android:layout_marginTop="18dp" >

            <RadioButton
                android:id="@+id/enable"
                android:layout_
                android:layout_
                android:text="Sensor Enable" />

            <RadioButton 
                android:id="@+id/disable"
                android:layout_
                android:layout_
                android:text="Sensor Disable" />
        </RadioGroup>

</RelativeLayout>

SensorTestActivity.java

import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class SensorTestActivity extends Activity implements SensorEventListener 
    private SensorManager sensorManager;
    private boolean color = false;
    private View view;
    private long lastUpdate;
    static boolean enableSensor = true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);
        view = findViewById(R.id.label);
        view.setBackgroundColor(Color.GREEN);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
        lastUpdate = System.currentTimeMillis();

        RadioGroup sensorSettings =(RadioGroup)findViewById(R.id.sensorSettings);
        sensorSettings.setOnCheckedChangeListener(new OnCheckedChangeListener()
            @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) 
            // checkedId is the RadioButton selected
            switch(checkedId) 
            case R.id.enable:
                enableSensor = true;
                break;
            case R.id.disable:
                enableSensor = false;
                break;
            
        
        );
    

    @Override
    public void onSensorChanged(SensorEvent event) 
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) 
            if(enableSensor)
                getAccelerometer(event);
                       
        

    

    private void getAccelerometer(SensorEvent event) 
        float[] values = event.values;
        // Movement
        float x = values[0];
        float y = values[1];
        float z = values[2];

        float accelationSquareRoot = (x * x + y * y + z * z)
                / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
        long actualTime = System.currentTimeMillis();
        if (accelationSquareRoot >= 2) //
        
            if (actualTime - lastUpdate < 200) 
                return;
            
            lastUpdate = actualTime;
            Toast.makeText(this, "Device has shaken", Toast.LENGTH_SHORT)
                    .show();
            if (color) 
                view.setBackgroundColor(Color.GREEN);

             else 
                view.setBackgroundColor(Color.RED);
            
            color = !color;
        
    

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

    

    @Override
    protected void onResume() 
        super.onResume();
        // register this class as a listener for the orientation and
        // accelerometer sensors
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
    

    @Override
    protected void onPause() 
        // unregister listener
        super.onPause();
        sensorManager.unregisterListener(this);
    

请添加 AndroidManifest 权限。

<uses-permission android:name="android.permission.VIBRATE" />

【讨论】:

谢谢!正是我想要的,+1 像魅力一样工作 我能再问你一件事,我应该如何参考传感器,所以当我点击一个按钮来激活和停用震动效果时 类似.. Switch.setOnClickListener(new View.OnClickListener() @Override public void onClick(View v) if (sensorIsOn) //deactivate it else //activate it ); 如果被激活,那么它会像上面那样做 您能否通过在该代码中添加一个开关点击侦听器来帮助我?提前致谢。 您好,很抱歉回复晚了。我添加了为您添加的开关 setOnCheckedChangeListener 监听器。请尝试让我知道。谢谢。

以上是关于震动传感器不工作的主要内容,如果未能解决你的问题,请参考以下文章

MMS-A01型三轴有线加速度传感器

proteus中有气体传感器吗?在transducers里没有找到呢

弦,震动的琴弦

扬声器的单位冲激响应信号建模

Android Studio学习路程(13)

proteus8里面有门磁传感器吗