App下載

在android中手機傳感器怎么調(diào)用?實現(xiàn)手機傳感器調(diào)用方法分享!

怪味少女 2021-08-21 16:31:17 瀏覽數(shù) (5421)
反饋

今天小編來和大家分享有關(guān)于“在android中手機傳感器怎么調(diào)用?”這個問題的使用方法和相關(guān)解決問題的思路,希望對大家的學(xué)習(xí)有所幫助! 

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,綁定監(jiān)聽器
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){
   //得到默認(rèn)的加速度傳感器
   Sensor lightSensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
   //綁定監(jiān)聽器(上下文接口,要監(jiān)聽的傳感器,傳感器采樣率<時間間隔>),返回結(jié)果
   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,SensorManager.SENSOR_DELAY_NORMAL);
   Toast.makeText(this,"綁定加速度傳感器:"+res,Toast.LENGTH_LONG).show();
  }else if(v==orientationBut){
   Sensor orientationSensor=sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
   Boolean res=sensorManager.registerListener(new OrientaationListener(),orientationSensor,SensorManager.SENSOR_DELAY_NORMAL);
   Toast.makeText(this,"綁定方向傳感器:"+res,Toast.LENGTH_LONG).show();
  }
  else if(v==proximityBut){
   Sensor proximitySensor=sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
   Boolean res=sensorManager.registerListener(new ProximityListener(),proximitySensor,SensorManager.SENSOR_DELAY_NORMAL);
   Toast.makeText(this,"綁定距離傳感器:"+res,Toast.LENGTH_LONG).show();
  }
 }
 
 public class LightSensorListener implements SensorEventListener{
  @Override
  //傳感器的數(shù)據(jù)被打包成event,主要的檢測數(shù)據(jù)放在enent.values[]數(shù)組中
  public void onSensorChanged(SensorEvent event) {
   System.out.println(event.timestamp);//時間戳
   System.out.println(event.sensor.getResolution());//分辨率(能識別出最小數(shù)值)
   System.out.println(event.accuracy);//精度(等級)
   System.out.println(event.values[0]);//光線強度
  }
  @Override
  //傳感器精度變化時調(diào)用這個函數(shù)
  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軸加速度,負(fù)方向為正
   //event.values[1]Y軸加速度,負(fù)方向為正
   //event.values[2]Z軸加速度,負(fù)方向為正
   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) {
   //(需要手機屏幕向上,向下的話南北會反掉)設(shè)備繞Z軸旋轉(zhuǎn),Y軸正方向與地磁北極方向的夾角,順時針方向為正,范圍【0,180】
   float azimuth=event.values[0];
   //設(shè)備繞X軸旋轉(zhuǎn)的角度,當(dāng)Z軸向Y軸正方向旋轉(zhuǎn)時為正,反之為負(fù),范圍【-180,180】
   float pitch=event.values[1];
   //設(shè)備繞Y軸旋轉(zhuǎn)的角度,當(dāng)Z軸向X軸正方向旋轉(zhuǎn)時為負(fù),反之為正,范圍【-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);
 }
}

界面截圖:

那么今天小編和大家分享的有關(guān)于“在android中手機傳感器怎么調(diào)用?”這個問題的解決方法我們都可以在W3Cschool中進(jìn)行學(xué)習(xí)和了解,更多有關(guān)于android的相關(guān)問題我們都可以在這個平臺中得到學(xué)習(xí)。


0 人點贊