1. 程式人生 > 程式設計 >android實現手機感測器呼叫

android實現手機感測器呼叫

android感測器使用的demo,包括光線感測器,加速度感測器,距離感測器和方向感測器。

demo:下載地址

原始碼:

package com.bobo.study.study_5_1;
 
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
import java.util.List;
//1,獲得SensorManager物件
//2,獲得想要的Sensor物件
//3,繫結監聽器
public class MainActivity extends Activity implements View.OnClickListener{
 Button findBut,accelerationBut,lightBut,orientationBut,proximityBut;
 SensorManager sensorManager;
 TextView text,accText,luxText;
 float gravity[]=new float[3];
 float linear_acceleration[]=new float[3];
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  findBut=(Button)findViewById(R.id.findBut);
  findBut.setOnClickListener(this);
  lightBut=(Button)findViewById(R.id.lightBut);
  lightBut.setOnClickListener(this);
  accelerationBut=(Button)findViewById(R.id.accelerationBut);
  accelerationBut.setOnClickListener(this);
  orientationBut=(Button)findViewById(R.id.orientationBut);
  orientationBut.setOnClickListener(this);
  proximityBut=(Button)findViewById(R.id.proximityBut);
  proximityBut.setOnClickListener(this);
 
  text=(TextView)findViewById(R.id.text);
  accText=(TextView)findViewById(R.id.accText);
  luxText=(TextView)findViewById(R.id.luxText);
 
  //獲得感測器管理器物件
  sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
 }
 
 @Override
 public void onClick(View v) {
  if(v==findBut){
   //獲取手機上所有感測器的列表
   List<Sensor> sensors=sensorManager.getSensorList(Sensor.TYPE_ALL);
   for(Sensor sensor:sensors){
    System.out.println(sensor.getName());
   }
  }else if(v==lightBut){
   //得到預設的加速度感測器
   Sensor lightSensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
   //繫結監聽器(上下文介面,要監聽的感測器,感測器取樣率<時間間隔>),返回結果
   Boolean res=sensorManager.registerListener(new LightSensorListener(),lightSensor,SensorManager.SENSOR_DELAY_NORMAL);
   Toast.makeText(this,"繫結光線感測器:"+res,Toast.LENGTH_LONG).show();
  }
  else if(v==accelerationBut){
   Sensor accelerometerSensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
   Boolean res=sensorManager.registerListener(new AccerationSensorListener(),accelerometerSensor,"繫結加速度感測器:"+res,Toast.LENGTH_LONG).show();
  }else if(v==orientationBut){
   Sensor orientationSensor=sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
   Boolean res=sensorManager.registerListener(new OrientaationListener(),orientationSensor,"繫結方向感測器:"+res,Toast.LENGTH_LONG).show();
  }
  else if(v==proximityBut){
   Sensor proximitySensor=sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
   Boolean res=sensorManager.registerListener(new ProximityListener(),proximitySensor,"繫結距離感測器:"+res,Toast.LENGTH_LONG).show();
  }
 }
 
 public class LightSensorListener implements SensorEventListener{
  @Override
  //感測器的資料被打包成event,主要的檢測資料放在enent.values[]陣列中
  public void onSensorChanged(SensorEvent event) {
   System.out.println(event.timestamp);//時間戳
   System.out.println(event.sensor.getResolution());//解析度(能識別出最小數值)
   System.out.println(event.accuracy);//精度(等級)
   System.out.println(event.values[0]);//光線強度
  }
  @Override
  //感測器精度變化時呼叫這個函式
  public void onAccuracyChanged(Sensor sensor,int accuracy) {}
 }
 
 public class AccerationSensorListener implements SensorEventListener{
  @Override
  public void onSensorChanged(SensorEvent event) {
   final float alpha=0.8f;
 
   //event.values[0]X軸加速度,負方向為正
   //event.values[1]Y軸加速度,負方向為正
   //event.values[2]Z軸加速度,負方向為正
   gravity[0]=alpha*gravity[0]+(1-alpha)*event.values[0];
   gravity[1]=alpha*gravity[1]+(1-alpha)*event.values[1];
   gravity[2]=alpha*gravity[2]+(1-alpha)*event.values[2];
 
   linear_acceleration[0]=event.values[0]-gravity[0];
   linear_acceleration[1]=event.values[1]-gravity[1];
   linear_acceleration[2]=event.values[2]-gravity[2];
 
   //通過以上公式可以拋去三個方向上的重力加速度,只剩下純加速度
   text.setText(linear_acceleration[0] + "");
   accText.setText(linear_acceleration[1] + "");
   luxText.setText(linear_acceleration[2] + "");
  }
  @Override
  public void onAccuracyChanged(Sensor sensor,int accuracy) {}
 }
 
 public class OrientaationListener implements SensorEventListener{
  @Override
  public void onSensorChanged(SensorEvent event) {
   //(需要手機螢幕向上,向下的話南北會反掉)裝置繞Z軸旋轉,Y軸正方向與地磁北極方向的夾角,順時針方向為正,範圍【0,180】
   float azimuth=event.values[0];
   //裝置繞X軸旋轉的角度,當Z軸向Y軸正方向旋轉時為正,反之為負,範圍【-180,180】
   float pitch=event.values[1];
   //裝置繞Y軸旋轉的角度,當Z軸向X軸正方向旋轉時為負,反之為正,範圍【-90,90】
   float roll=event.values[2];
 
   text.setText(azimuth+"");
   accText.setText(pitch +"");
   luxText.setText(roll+"");
  }
  @Override
  public void onAccuracyChanged(Sensor sensor,int accuracy) {}
 }
 
 public class ProximityListener implements SensorEventListener{
  @Override
  public void onSensorChanged(SensorEvent event) {
   //距離感測器測試手機螢幕距離別的物體的記錄,只有兩個值:0和5
   //距離很近時為0,否則為5
   System.out.println(event.values[0]+"");
  }
  @Override
  public void onAccuracyChanged(Sensor sensor,int accuracy) {}
 }
 
 
 
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.menu_main,menu);
  return true;
 }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button,so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  //noinspection SimplifiableIfStatement
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
}

介面截圖:

android實現手機感測器呼叫

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。