【移動(dòng)應(yīng)用開發(fā)技術(shù)】Identifying Sensors and Sensor Capabilities_第1頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Identifying Sensors and Sensor Capabilities_第2頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Identifying Sensors and Sensor Capabilities_第3頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Identifying Sensors and Sensor Capabilities_第4頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Identifying Sensors and Sensor Capabilities_第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論