小編這次要給大家分享的是Android如何實(shí)現(xiàn)接近傳感器,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

創(chuàng)新互聯(lián)專注于蘭西網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供蘭西營銷型網(wǎng)站建設(shè),蘭西網(wǎng)站制作、蘭西網(wǎng)頁設(shè)計(jì)、蘭西網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造蘭西網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供蘭西網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
1.接近傳感器檢測物體與聽筒(手機(jī))的距離,單位是厘米。
一些接近傳感器只能返回遠(yuǎn)和近兩個(gè)狀態(tài),如我的手機(jī)魅族E2只能識別到兩個(gè)距離:0CM(近距離)和5CM(遠(yuǎn)距離)
因此,接近傳感器將最大距離返回遠(yuǎn)狀態(tài),小于最大距離返回近狀態(tài)。
接近傳感器可用于接聽電話時(shí)自動關(guān)閉LCD屏幕以節(jié)省電量。
一些芯片集成了接近傳感器和光線傳感器兩者功能(魅族E2)。
2.代碼如下:
MainActivity.class
package com.example.sz.proximitytest;
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.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private SensorManager mSensorManager=null;
private Sensor mSensor=null;
private TextView textView1=null;
private TextView textView2=null;
private TextView textView3=null;
private Button button1=null;
private Button button2=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);
/*獲取系統(tǒng)服務(wù)(SENSOR_SERVICE)返回一個(gè)SensorManager對象*/
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
/*通過SensorManager獲取相應(yīng)的(接近傳感器)Sensor類型對象*/
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
/*注冊相應(yīng)的SensorService*/
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
mSensorManager.registerListener(mSensorEventListener, mSensor
, SensorManager.SENSOR_DELAY_NORMAL);
}
});
/* 銷毀相應(yīng)的SensorService
* 很關(guān)鍵的部分,注意,說明文檔中提到,即使Activity不可見的時(shí)候,感應(yīng)器依然會繼續(xù)工作
* 所以一定要關(guān)閉觸發(fā)器,否則將消耗用戶大量電量*/
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
mSensorManager.unregisterListener(mSensorEventListener, mSensor);
}
});
}
/*聲明一個(gè)SensorEventListener對象用于偵聽Sensor事件,并重載onSensorChanged方法*/
private final SensorEventListener mSensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
Log.e(TAG, "onSensorChanged: -----0------"+event.values[0]);
Log.e(TAG, "onSensorChanged: ------1-----"+event.values[1]);
Log.e(TAG, "onSensorChanged: --------2---"+event.values[2]);
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
/*接近傳感器檢測物體與聽筒的距離,單位是厘米。*/
//這里要注意,正常都是取第一位的值,但我碰到一個(gè)取第二位的
float distance1 = event.values[0];
float distance2 = event.values[1];
float distance3 = event.values[2];
textView1.setText("[0]距離:"+String.valueOf(distance1) + "cm");
textView2.setText("[1]距離:"+String.valueOf(distance2) + "cm");
textView3.setText("[2]距離:"+String.valueOf(distance3) + "cm");
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
}activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="打開" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="關(guān)閉" /> </LinearLayout>
看完這篇關(guān)于Android如何實(shí)現(xiàn)接近傳感器的文章,如果覺得文章內(nèi)容寫得不錯(cuò)的話,可以把它分享出去給更多人看到。
分享名稱:Android如何實(shí)現(xiàn)接近傳感器
文章源于:http://www.chinadenli.net/article36/pgcjpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、響應(yīng)式網(wǎng)站、網(wǎng)站制作、網(wǎng)站營銷、App設(shè)計(jì)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)