![【移動(dòng)應(yīng)用開發(fā)技術(shù)】Identifying Sensors and Sensor Capabilities_第1頁](http://file4.renrendoc.com/view/6ae01982c128cdb34901b6db47c3d883/6ae01982c128cdb34901b6db47c3d8831.gif)
![【移動(dòng)應(yīng)用開發(fā)技術(shù)】Identifying Sensors and Sensor Capabilities_第2頁](http://file4.renrendoc.com/view/6ae01982c128cdb34901b6db47c3d883/6ae01982c128cdb34901b6db47c3d8832.gif)
![【移動(dòng)應(yīng)用開發(fā)技術(shù)】Identifying Sensors and Sensor Capabilities_第3頁](http://file4.renrendoc.com/view/6ae01982c128cdb34901b6db47c3d883/6ae01982c128cdb34901b6db47c3d8833.gif)
![【移動(dòng)應(yīng)用開發(fā)技術(shù)】Identifying Sensors and Sensor Capabilities_第4頁](http://file4.renrendoc.com/view/6ae01982c128cdb34901b6db47c3d883/6ae01982c128cdb34901b6db47c3d8834.gif)
![【移動(dòng)應(yīng)用開發(fā)技術(shù)】Identifying Sensors and Sensor Capabilities_第5頁](http://file4.renrendoc.com/view/6ae01982c128cdb34901b6db47c3d883/6ae01982c128cdb34901b6db47c3d8835.gif)
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】IdentifyingSensorsandSensorCapabilities
IdentifyingSensorsandSensorCapabilities識(shí)別傳感器及其功能TheAndroidsensorframeworkprovidesseveralmethodsthatmakeiteasyforyoutodetermineat
runtimewhichsensorsareonadevice.TheAPIalsoprovidesmethodsthatletyoudeterminethe
capabilitiesofeachsensor,suchasitsmaximumrange,itsresolution,anditspower
requirements.Toidentifythesensorsthatareonadeviceyoufirstneedtogetareferencetothesensor
service.Todothis,youcreateaninstanceoftheSensorManagerclassby
callingthegetSystemService()methodandpassing
intheSENSOR_SERVICEargument.Forexample:
識(shí)別設(shè)備上的傳感器你首先需要得到sensorservice的一個(gè)引用。為了得到這個(gè)引用,你調(diào)用getSystemService()方法并傳遞SENSOR_SERVICE參數(shù)來獲得SensorManager類的實(shí)例。private
SensorManager
mSensorManager;
...
mSensorManager
=
(SensorManager)
getSystemService(Context.SENSOR_SERVICE);Next,youcangetalistingofeverysensoronadevicebycallingthegetSensorList()methodandusingtheTYPE_ALLconstant.Forexample:然后你可以用getSensorList()列舉設(shè)備上所有傳感器。List<Sensor>
deviceSensors
=
mSensorManager.getSensorList(Sensor.TYPE_ALL);Ifyouwanttolistallofthesensorsofagiventype,youcoulduseanotherconstantinsteadofTYPE_ALLsuchasTYPE_GYROSCOPE,TYPE_LINEAR_ACCELERATION,orTYPE_GRAVITY.也可傳遞其他參數(shù)類列列舉特定類型的所有傳感器。YoucanalsodeterminewhetheraspecifictypeofsensorexistsonadevicebyusingthegetDefaultSensor()methodandpassinginthetype
constantforaspecificsensor.Ifadevicehasmorethanonesensorofagiventype,oneofthe
sensorsmustbedesignatedasthedefaultsensor.Ifadefaultsensordoesnotexistforagiven
typeofsensor,themethodcallreturnsnull,whichmeansthedevicedoesnothavethattypeof
sensor.Forexample,thefollowingcodecheckswhetherthere'samagnetometeronadevice:你也可以用getDefaultSensor(),傳遞特定傳感器的類型常量來確定某種傳感器是否在設(shè)備上。如果這種傳感器不止一個(gè),一定指定其中一個(gè)為默認(rèn)的傳感器。如果某個(gè)特定類型的默認(rèn)傳感器不存在,這個(gè)方法返回null,表示設(shè)備上不存在這種傳感器。例如以下的代碼檢測(cè)設(shè)備上是否有磁力計(jì)。private
SensorManager
mSensorManager;
...
mSensorManager
=
(SensorManager)
getSystemService(Context.SENSOR_SERVICE);
if
(mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
!=
null){
//
Success!
There's
a
magnetometer.
}
else
{
//
Failure!
No
magnetometer.
}Note:Androiddoesnotrequiredevicemanufacturerstobuildany
particulartypesofsensorsintotheirAndroid-powereddevices,sodevicescanhaveawiderangeof
sensorconfigurations.Android不強(qiáng)求制造商在他們的Android設(shè)備上搭載某些傳感器,所以設(shè)備上的傳感器類別并不固定。Inadditiontolistingthesensorsthatareonadevice,youcanusethepublicmethodsoftheSensorclasstodeterminethecapabilitiesandattributesofindividual
sensors.Thisisusefulifyouwantyourapplicationtobehavedifferentlybasedonwhichsensorsor
sensorcapabilitiesareavailableonadevice.Forexample,youcanusethegetResolution()andgetMaximumRange()methodstoobtainasensor'sresolutionandmaximumrangeofmeasurement.YoucanalsousethegetPower()methodtoobtainasensor'spowerequirements.你不僅可以列舉設(shè)備上的所有傳感器,還可以用Sensor類的公共方法確定傳感器的功能和屬性。如果你的應(yīng)用根據(jù)某個(gè)傳感器或者某些傳感器功能而有不同行為,這會(huì)非常有用。例如,用getResolution()和getMaximumRange()方法獲得一個(gè)探測(cè)器的分辨率和測(cè)量的最大范圍。你也可以用getPower()方法獲得傳感器的電力需求。Twoofthepublicmethodsareparticularlyusefulifyouwanttooptimizeyourapplicationfor
differentmanufacturer'ssensorsordifferentversionsofasensor.Forexample,ifyourapplication
needstomonitorusergesturessuchastiltandshake,youcouldcreateonesetofdatafiltering
rulesandoptimizationsfornewerdevicesthathaveaspecificvendor'sgravitysensor,andanother
setofdatafilteringrulesandoptimizationsfordevicesthatdonothaveagravitysensorandhave
onlyanaccelerometer.ThefollowingcodesampleshowsyouhowyoucanusethegetVendor()andgetVersion()methodstodo
this.Inthissample,we'relookingforagravitysensorthatlistsGoogleInc.asthevendorand
hasaversionnumberof3.Ifthatparticularsensorisnotpresentonthedevice,wetrytousethe
accelerometer.private
SensorManager
mSensorManager;
private
Sensor
mSensor;
...
mSensorManager
=
(SensorManager)
getSystemService(Context.SENSOR_SERVICE);
if
(mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)
!=
null){
List<Sensor>
gravSensors
=
mSensorManager.getSensorList(Sensor.TYPE_GRAVITY);
for(int
i=0;
i<gravSensors.size();
i++)
{
if
((gravSensors.get(i).getVendor().contains("Google
Inc."))
&&
(gravSensors.get(i).getVersion()
==
3)){
//
Use
the
version
3
gravity
sensor.
mSensor
=
gravSensors.get(i);
}
}
}
else{
//
Use
the
accelerometer.
if
(mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
!=
null){
mSensor
=
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
else{
//
Sorry,
there
are
no
accelerometers
on
your
device.
//
You
can't
play
this
game.
}
}AnotherusefulmethodisthegetMinDelay()method,
whichreturnstheminimumtimeinterval(inmicroseconds)asensorcanusetosensedata.Anysensor
thatreturnsanon-zerovalueforthegetMinDelay()methodisastreaming
sensor.StreamingsensorssensedataatregularintervalsandwereintroducedinAndroid2.3(API
Level9).IfasensorreturnszerowhenyoucallthegetMinDelay()method,itmeansthe
sensorisnotastreamingsensorbecauseitreportsdataonlywhenthereisachangeinthe
parametersitissensing.ThegetMinDelay()methodisusefulbecauseitlets
youdeterminethemaximumrate
atwhichasensorcanacquire
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 活法讀后感(精彩5篇)
- 2025年云南省職教高考《職業(yè)適應(yīng)性測(cè)試》考前沖刺模擬試題庫(附答案)
- 《內(nèi)容策劃與編輯》期末考試題庫及答案
- 第一章 地球(單元測(cè)試)(解析版)
- 2025年江西泰豪動(dòng)漫職業(yè)學(xué)院高職單招職業(yè)技能測(cè)試近5年??及鎱⒖碱}庫含答案解析
- 陜西省寶雞市高三教學(xué)質(zhì)量檢測(cè)語文試題(含答案)
- 2025年民用航空運(yùn)輸行業(yè)趨勢(shì)與市場(chǎng)潛力分析
- 勞務(wù)分包合同零工
- 假期守校合同協(xié)議書
- 合同范本之采購合同中英文對(duì)照
- 四川省自貢市2024-2025學(xué)年上學(xué)期八年級(jí)英語期末試題(含答案無聽力音頻及原文)
- 2025年上海用人單位勞動(dòng)合同(4篇)
- 新疆烏魯木齊地區(qū)2025年高三年級(jí)第一次質(zhì)量監(jiān)測(cè)生物學(xué)試卷(含答案)
- 衛(wèi)生服務(wù)個(gè)人基本信息表
- 高中英語北師大版必修第一冊(cè)全冊(cè)單詞表(按單元編排)
- 苗圃建設(shè)項(xiàng)目施工組織設(shè)計(jì)范本
- 通用電子嘉賓禮薄
- (完整word版)英語四級(jí)單詞大全
- 武裝押運(yùn)操作規(guī)程完整
- 薪酬專員崗位月度KPI績(jī)效考核表
- 技能大賽題庫(空分)
評(píng)論
0/150
提交評(píng)論