版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第Android獲取設(shè)備傳感器的方法本文實(shí)例為大家分享了Android獲取設(shè)備傳感器的具體代碼,供大家參考,具體內(nèi)容如下
結(jié)果示例:
xml代碼:
xmlversion="1.0"encoding="utf-8"
RelativeLayoutxmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="/apk/res-auto"
tools:context=".MainActivity"
LinearLayout
android:id="@+id/liner"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="text"/
TextView
android:id="@+id/accText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="accText"/
TextView
android:id="@+id/luxText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="luxText"/
/LinearLayout
GridLayout
android:id="@+id/gl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="1"
android:padding="20dp"
android:rowCount="5"
android:layout_below="@+id/liner"
Button
android:id="@+id/findAllSensorBut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="獲取所有傳感器列表"/
Button
android:id="@+id/lightBut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="光線傳感器"/
Button
android:id="@+id/accelerationBut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="加速度傳感器"/
Button
android:id="@+id/orientationBut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="方向傳感器"/
Button
android:id="@+id/proximityBut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="距離傳感器"/
/GridLayout
LinearLayout
android:id="@+id/lsLiner"
android:orientation="vertical"
android:layout_below="@+id/gl"
android:layout_width="match_parent"
android:layout_height="match_parent"
TextView
android:text="傳感器列表:"
android:layout_width="match_parent"
android:layout_height="wrap_content"/
ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/
/LinearLayout
/RelativeLayout
java代碼:
packagecom.chy.myActivity;
importandroidx.appcompat.app.AppCompatActivity;
importandroid.Manifest;
importandroid.content.Context;
importandroid.hardware.Sensor;
importandroid.hardware.SensorEvent;
importandroid.hardware.SensorEventListener;
importandroid.hardware.SensorManager;
importandroid.os.Bundle;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.widget.ArrayAdapter;
importandroid.widget.Button;
importandroid.widget.ListView;
importandroid.widget.TextView;
importandroid.widget.Toast;
importjava.util.ArrayList;
importjava.util.List;
publicclassMainActivityextendsAppCompatActivityimplementsView.OnClickListener{
//動(dòng)態(tài)申請權(quán)限
privateString[]permissions={
Manifest.permission.INTERNET,//網(wǎng)絡(luò)權(quán)限
Manifest.permission.CAMERA,//相機(jī)權(quán)限
Manifest.permission.RECORD_AUDIO,//音頻錄制權(quán)限
Manifest.permission.ACCESS_FINE_LOCATION,//定位權(quán)限
Manifest.permission.WRITE_EXTERNAL_STORAGE,//寫入數(shù)據(jù)權(quán)限
Manifest.permission.READ_EXTERNAL_STORAGE,//讀取數(shù)據(jù)權(quán)限
Manifest.permission.ACCESS_COARSE_LOCATION//獲取基站的服務(wù)信號(hào)權(quán)限,以便獲取位置信息
};
privateButtonfindAllSensorBut;//所有傳感器列表
privateButtonlightBut;//光線傳感器
privateButtonaccelerationBut;//加速度傳感器
privateButtonorientationBut;//方向傳感器
privateButtonproximityBut;//距離傳感器
privateSensorManagersensorManager;//傳感器管理器對(duì)象
privateTextViewtext;
privateTextViewaccText;
privateTextViewluxText;
privatefloatgravity[]=newfloat[3];
privatefloatlinear_acceleration[]=newfloat[3];
privateListViewlistView;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//獲得傳感器管理器對(duì)象
sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
}
privatevoidinitView(){
text=findViewById(R.id.text);
accText=findViewById(R.id.accText);
luxText=findViewById(R.id.luxText);
//所有傳感器列表
findAllSensorBut=findViewById(R.id.findAllSensorBut);
findAllSensorBut.setOnClickListener(this);
//光線傳感器
lightBut=findViewById(R.id.lightBut);
lightBut.setOnClickListener(this);
//加速度傳感器
accelerationBut=findViewById(R.id.accelerationBut);
accelerationBut.setOnClickListener(this);
//方向傳感器
orientationBut=findViewById(R.id.orientationBut);
orientationBut.setOnClickListener(this);
//距離傳感器
proximityBut=findViewById(R.ximityBut);
listView=findViewById(R.id.lv);
}
/**
*按鈕點(diǎn)擊事件
**/
@Override
publicvoidonClick(Viewv){
switch(v.getId()){
caseR.id.findAllSensorBut://傳感器列表
//數(shù)據(jù)
ArrayListStringdata=newArrayList();
//獲取設(shè)備中所有傳感器
ListSensorsensors=sensorManager.getSensorList(Sensor.TYPE_ALL);
for(Sensorsensor:sensors){
System.out.println("傳感器:"+sensor.getName());
data.add(sensor.getName());
}
ArrayAdapteradapter=newArrayAdapter(this,android.R.layout.simple_list_item_1,data);
listView.setAdapter(adapter);
break;
caseR.id.lightBut://光線傳感器
//得到默認(rèn)的光線傳感器
SensorlightSensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
//綁定監(jiān)聽器(上下文接口,要監(jiān)聽的傳感器,傳感器采樣率時(shí)間間隔),返回結(jié)果
Booleanlight_res=sensorManager.registerListener(newLightSensorListener(),lightSensor,SensorManager.SENSOR_DELAY_NORMAL);
Toast.makeText(this,"綁定光線傳感器:"+light_res,Toast.LENGTH_LONG).show();
break;
caseR.id.accelerationBut://加速度傳感器
SensoraccelerometerSensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Booleanacceleration_res=sensorManager.registerListener(newAccerationSensorListener(),accelerometerSensor,SensorManager.SENSOR_DELAY_NORMAL);
Toast.makeText(this,"綁定加速度傳感器:"+acceleration_res,Toast.LENGTH_LONG).show();
break;
caseR.id.orientationBut://方向傳感器
SensororientationSensor=sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
Booleanorient_res=sensorManager.registerListener(newOrientaationListener(),orientationSensor,SensorManager.SENSOR_DELAY_NORMAL);
Toast.makeText(this,"綁定方向傳感器:"+orient_res,Toast.LENGTH_LONG).show();
break;
caseR.ximityBut://距離傳感器
SensorproximitySensor=sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
Booleanproximity_res=sensorManager.registerListener(newProximityListener(),proximitySensor,SensorManager.SENSOR_DELAY_NORMAL);
Toast.makeText(this,"綁定距離傳感器:"+proximity_res,Toast.LENGTH_LONG).show();
break;
}
}
/**
*光線傳感器類
**/
publicclassLightSensorListenerimplementsSensorEventListener{
@Override
//傳感器的數(shù)據(jù)被打包成event,主要的檢測數(shù)據(jù)放在enent.values[]數(shù)組中
publicvoidonSensorChanged(SensorEventevent){
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ù)
publicvoidonAccuracyChanged(Sensorsensor,intaccuracy){}
}
/**
*加速度傳感器類
**/
publicclassAccerationSensorListenerimplementsSensorEventListener{
@Override
publicvoidonSensorChanged(SensorEventevent){
finalfloatalpha=0.8f;
//event.values[0]X軸加速度,負(fù)方向?yàn)檎?/p>
//event.values[1]Y軸加速度,負(fù)方向?yàn)檎?/p>
//event.values[2]Z軸加速度,負(fù)方向?yàn)檎?/p>
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
publicvoidonAccuracyChanged(Sensorsensor,intaccuracy){}
}
/**
*方向傳感器類
**/
publicclassOrientaati
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二硫化碳生產(chǎn)工測試驗(yàn)證評(píng)優(yōu)考核試卷含答案
- 電力通信運(yùn)維員崗前規(guī)章制度考核試卷含答案
- 片基流延工誠信道德能力考核試卷含答案
- 電子玻璃制品鍍膜工安全宣教測試考核試卷含答案
- 安全員考試請假條
- 2025年超細(xì)銀粉末、銀鈀粉、鈀粉、鉑粉項(xiàng)目合作計(jì)劃書
- 2026年智能心率帶項(xiàng)目營銷方案
- 2025年江蘇省南通市中考物理真題卷含答案解析
- 2025年山東省日照市中考英語真題卷含答案解析
- 2025康復(fù)醫(yī)學(xué)與技術(shù)專業(yè)知識(shí)題庫及答案
- 招標(biāo)代理機(jī)構(gòu)入圍 投標(biāo)方案(技術(shù)方案)
- 運(yùn)輸車隊(duì)年終總結(jié)報(bào)告
- 房屋損壞糾紛鑒定報(bào)告
- 精益生產(chǎn)方式-LEAN-PRODUCTION
- 中學(xué)體育與健康課程與教學(xué)論P(yáng)PT高職完整全套教學(xué)課件
- 頸動(dòng)脈外膜剝脫術(shù)
- 養(yǎng)老設(shè)施建筑設(shè)計(jì)規(guī)范
- Starter-軟件簡易使用手冊
- RFJ01-2008 人民防空工程防護(hù)設(shè)備選用圖集
- GB/T 27818-2011化學(xué)品皮膚吸收體外試驗(yàn)方法
- FZ/T 80004-2014服裝成品出廠檢驗(yàn)規(guī)則
評(píng)論
0/150
提交評(píng)論