Android獲取設(shè)備傳感器的方法_第1頁
Android獲取設(shè)備傳感器的方法_第2頁
Android獲取設(shè)備傳感器的方法_第3頁
Android獲取設(shè)備傳感器的方法_第4頁
Android獲取設(shè)備傳感器的方法_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

評(píng)論

0/150

提交評(píng)論