今天小編來(lái)和大家分享有關(guān)于“在android中手機(jī)傳感器怎么調(diào)用?”這個(gè)問(wèn)題的使用方法和相關(guān)解決問(wèn)題的思路,希望對(duì)大家的學(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對(duì)象 //2,獲得想要的Sensor對(duì)象 //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); //獲得傳感器管理器對(duì)象 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,主要的檢測(cè)數(shù)據(jù)放在enent.values[]數(shù)組中 public void onSensorChanged(SensorEvent event) { System.out.println(event.timestamp);//時(shí)間戳 System.out.println(event.sensor.getResolution());//分辨率(能識(shí)別出最小數(shù)值) System.out.println(event.accuracy);//精度(等級(jí)) 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]; //通過(guò)以上公式可以拋去三個(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í)針?lè)较驗(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) { //距離傳感器測(cè)試手機(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); } }
界面截圖:
那么今天小編和大家分享的有關(guān)于“在android中手機(jī)傳感器怎么調(diào)用?”這個(gè)問(wèn)題的解決方法我們都可以在W3Cschool中進(jìn)行學(xué)習(xí)和了解,更多有關(guān)于android的相關(guān)問(wèn)題我們都可以在這個(gè)平臺(tái)中得到學(xué)習(xí)。
您可能感興趣的內(nèi)容和課程: