2013-07-31 46 views
8

我想在我的項目中使用接近傳感器。我搜索了接近傳感器教程。我在http://www.vogella.com/articles/AndroidSensor/article.html#sensoroverview_manager找到了一個傳感器教程。我試圖用接近傳感器爲下面的代碼:在android中使用接近傳感器

@Override 
public void onSensorChanged(SensorEvent event) 
{ 
    if(event.sensor.getType() == Sensor.TYPE_PROXIMITY) 
    { 
     Toast.makeText(getApplicationContext(), "working", Toast.LENGTH_SHORT).show(); 
    } 
} 

Activity正在實施SensorEventListener。但它不工作。我是否需要使用任何權限才能使用接近傳感器?或者我的代碼是錯誤的做這個東西。您的幫助將非常感激。謝謝

+0

你能告訴設置代碼? – Ifor

+2

在android上有**接近傳感器**的多個教程。首先,通過閱讀這篇[關於Android **上的接近傳感器的文章](http://thecodeartist.blogspot.in/2011/01/proximity-sensor-on-android-gingerbread.html)來開發正確的理解。接下來,以下教程[** 1 **](http://androidbite.blogspot.in/2013/05/android-proximity-sensor-example.html)和[** 2 **](http:// velmuruganandroidcoding.blogspot.in/2013/05/proximity-sensor-in-android-example.html)應該有所幫助。另外我會建議你在具有接近傳感器的真實設備上運行你的代碼,因爲它是非常直接的 – Rahil2952

+0

運行示例http://androidbite.blogspot.com/2013/05/android-proximity-sensor-example。 html –

回答

8

我用這個代碼:

import android.content.Context; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity implements SensorEventListener { 

    private SensorManager mSensorManager; 
    private Sensor mProximity; 
    private static final int SENSOR_SENSITIVITY = 4; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
     mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     mSensorManager.unregisterListener(this); 
    } 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) { 
      if (event.values[0] >= -SENSOR_SENSITIVITY && event.values[0] <= SENSOR_SENSITIVITY) { 
       //near 
       Toast.makeText(getApplicationContext(), "near", Toast.LENGTH_SHORT).show(); 
      } else { 
       //far 
       Toast.makeText(getApplicationContext(), "far", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 

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

    } 

} 

你也可以check this code in GitHub

+1

這是不太可能的工作。我的設備(Android 5.1.1上的Moto X 2013)報告'3.0'爲接近狀態,所以此代碼將始終顯示「遠」 –

+1

您好Marcin Kozinski,我有同樣的問題,您可以檢查我的其他問題和答案[在這裏](http://stackoverflow.com/questions/35767115/unregisterlistener-doesnt-work-with-proximity-screen-off-wake-lock)。我希望我能幫助你。 – Cabezas

+1

你確定你已經鏈接到正確的問題/答案嗎?它看起來沒有關係,並且存在相同的問題:假定「接近」始終爲「0」,因此在某些電話上它永遠不會報告「接近」。現在我只是檢查'if(event.values [0]

2

你要註冊你的聽衆。在您的活動:

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

    // Get an instance of the sensor service, and use that to get an instance of 
    // a particular sensor. 
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
    mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); 
} 

@Override 
protected void onResume() { 
    // Register a listener for the sensor. 
    super.onResume(); 
    mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL); 
} 

,然後在你被覆蓋的onSensorChanged()方法,你可以用傳感器數據你的東西。

如果我的回答是爲時已晚對不起=)

0

您可以follow this code其中有使用它我們可以計劃的基本框架任何傳感器。

改變這一行

linearAcceleration = senseManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION); 

proximity = senseManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);