android實(shí)現(xiàn)手機(jī)傳感器調(diào)用
更新時(shí)間:2020年04月20日 09:20:11 作者:風(fēng)之盔
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)手機(jī)傳感器調(diào)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
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,綁定監(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){ //獲取手機(jī)上所有傳感器的列表 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)聽的傳感器,傳感器采樣率<時(shí)間間隔>),返回結(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);//時(shí)間戳 System.out.println(event.sensor.getResolution());//分辨率(能識別出最小數(shù)值) System.out.println(event.accuracy);//精度(等級) System.out.println(event.values[0]);//光線強(qiáng)度 } @Override //傳感器精度變化時(shí)調(diào)用這個(gè)函數(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ù)方向?yàn)檎? //event.values[1]Y軸加速度,負(fù)方向?yàn)檎? //event.values[2]Z軸加速度,負(fù)方向?yàn)檎? 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]; //通過以上公式可以拋去三個(gè)方向上的重力加速度,只剩下純加速度 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) { //(需要手機(jī)屏幕向上,向下的話南北會(huì)反掉)設(shè)備繞Z軸旋轉(zhuǎn),Y軸正方向與地磁北極方向的夾角,順時(shí)針方向?yàn)檎秶?,180】 float azimuth=event.values[0]; //設(shè)備繞X軸旋轉(zhuǎn)的角度,當(dāng)Z軸向Y軸正方向旋轉(zhuǎn)時(shí)為正,反之為負(fù),范圍【-180,180】 float pitch=event.values[1]; //設(shè)備繞Y軸旋轉(zhuǎn)的角度,當(dāng)Z軸向X軸正方向旋轉(zhuǎn)時(shí)為負(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) { //距離傳感器測試手機(jī)屏幕距離別的物體的記錄,只有兩個(gè)值:0和5 //距離很近時(shí)為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); } }
界面截圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)底部導(dǎo)航欄功能(選項(xiàng)卡)
這篇文章主要介紹了Android實(shí)現(xiàn)底部導(dǎo)航欄功能,可以隨意切換不同的頁面,實(shí)現(xiàn)選項(xiàng)卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-12-12Android Studio升級3.6 Build窗口出現(xiàn)中文亂碼問題解決方法
這篇文章主要介紹了Android Studio升級3.6 Build窗口出現(xiàn)中文亂碼問題解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Flutter封裝組動(dòng)畫混合動(dòng)畫AnimatedGroup示例詳解
這篇文章主要為大家介紹了Flutter封裝組動(dòng)畫混合動(dòng)畫AnimatedGroup示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android自定義RadioGroupX實(shí)現(xiàn)多行多列布局
這篇文章主要為大家詳細(xì)介紹了Android自定義RadioGroupX實(shí)現(xiàn)多行多列布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09