Android动态获取传感器数据
发布时间
阅读量:
阅读量
开发环境:Android studio 2.2.1
项目使用自带的Scrolling Activity
附上布局文件,我随便写的,懒得用include
<?**xml version=****"1.0"****encoding=****"utf-8"**?>
**android.support.v4.widget.NestedScrollView****xmlns:****android****=****"http://schemas.android.com/apk/res/android"********xmlns:****app****=****"http://schemas.android.com/apk/res-auto"********xmlns:****tools****=****"http://schemas.android.com/tools"********android****:layout_width=****"match_parent"********android****:layout_height=****"match_parent"********app****:layout_behavior=****"@string/appbar_scrolling_view_behavior"********tools****:context=****"com.example.****************.sensor.ScrollingActivity"********tools****:showIn=****"@layout/activity_scrolling"****LinearLayout********android****:orientation=****"vertical"********android****:layout_width=****"match_parent"********android****:layout_height=****"wrap_content"****LinearLayout********android****:orientation=****"vertical"********android****:layout_width=****"match_parent"********android****:layout_height=****"wrap_content"****LinearLayout********android****:orientation=****"vertical"********android****:gravity=****"center_horizontal"********android****:layout_width=****"match_parent"********android****:background=****"@android:color/holo_orange_light"********android****:layout_height=****"wrap_content"********android****:layout_margin=****"16dp"****ImageView************android****:layout_width=****"wrap_content"********android****:layout_height=****"wrap_content"********app****:srcCompat=****"@drawable/pressures"********android****:id=****"@+id/imageView"****TextView********android****:text=****"压力"********android****:layout_width=****"match_parent"********android****:layout_height=****"wrap_content"********android****:id=****"@+id/textView"********android****:textSize=****"18dp"********android****:textStyle=****"normal|bold"********android****:layout_marginLeft=****"10dp"********android****:layout_marginBottom=****"10dp"********android****:textColor=****"@android:color/background_light"****LinearLayout****LinearLayout********android****:orientation=****"vertical"********android****:layout_width=****"match_parent"********android****:layout_height=****"match_parent"********android****:paddingLeft=****"16dp"********android****:paddingRight=****"16dp"********android****:background=****"@android:color/background_light"****TextView********android****:text=****"hPa ***"********android****:layout_width=****"match_parent"********android****:layout_height=****"wrap_content"********android****:id=****"@+id/textView2"********android****:textStyle=****"normal|bold"********android****:textSize=****"18dp"****LinearLayout****LinearLayout****LinearLayout********android****:orientation=****"vertical"********android****:layout_width=****"match_parent"********android****:layout_height=****"wrap_content"********android****:layout_marginTop=****"30dp"****LinearLayout********android****:orientation=****"vertical"********android****:gravity=****"center_horizontal"********android****:layout_width=****"match_parent"********android****:background=****"?android:attr/colorControlHighlight"********android****:layout_height=****"wrap_content"********android****:layout_margin=****"16dp"****ImageView************android****:layout_width=****"wrap_content"********android****:layout_height=****"wrap_content"********app****:srcCompat=****"@drawable/light"********android****:id=****"@+id/imageView2"****TextView********android****:text=****"光照"********android****:layout_width=****"match_parent"********android****:layout_height=****"wrap_content"********android****:id=****"@+id/textView3"********android****:textSize=****"18dp"********android****:textStyle=****"normal|bold"********android****:layout_marginLeft=****"10dp"********android****:layout_marginBottom=****"10dp"********android****:textColor=****"@android:color/background_light"****LinearLayout****LinearLayout********android****:orientation=****"vertical"********android****:layout_width=****"match_parent"********android****:layout_height=****"match_parent"********android****:paddingLeft=****"16dp"********android****:paddingRight=****"16dp"********android****:background=****"@android:color/background_light"****TextView********android****:text=****"lx ***"********android****:layout_width=****"match_parent"********android****:layout_height=****"wrap_content"********android****:id=****"@+id/textView4"********android****:textStyle=****"normal|bold"********android****:textSize=****"18dp"****LinearLayout****LinearLayout****LinearLayout********android****:orientation=****"vertical"********android****:layout_width=****"match_parent"********android****:layout_height=****"match_parent"****LinearLayout********android****:orientation=****"vertical"********android****:gravity=****"center_horizontal"********android****:layout_width=****"match_parent"********android****:background=****"@android:color/holo_orange_light"********android****:layout_height=****"wrap_content"********android****:layout_margin=****"16dp"****ImageView************android****:layout_width=****"wrap_content"********android****:layout_height=****"wrap_content"********app****:srcCompat=****"@drawable/battery_full"********android****:id=****"@+id/imageView3"****TextView********android****:text=****"电池"********android****:layout_width=****"match_parent"********android****:layout_height=****"wrap_content"********android****:id=****"@+id/textView5"********android****:textSize=****"18dp"********android****:textStyle=****"normal|bold"********android****:layout_marginLeft=****"10dp"********android****:layout_marginBottom=****"10dp"********android****:textColor=****"@android:color/background_light"****LinearLayout****LinearLayout********android****:orientation=****"vertical"********android****:layout_width=****"match_parent"********android****:layout_height=****"match_parent"********android****:paddingLeft=****"16dp"********android****:paddingRight=****"16dp"********android****:background=****"@android:color/background_light"****TextView********android****:text=****"电量级别: **%"********android****:layout_width=****"match_parent"********android****:layout_height=****"wrap_content"********android****:id=****"@+id/textView6"********android****:textStyle=****"normal|bold"********android****:textSize=****"18dp"****TextView********android****:text=****"温度: **"********android****:layout_width=****"match_parent"********android****:layout_height=****"wrap_content"********android****:id=****"@+id/textView7"********android****:textStyle=****"normal|bold"********android****:textSize=****"18dp"****LinearLayout****LinearLayout****LinearLayout****android.support.v4.widget.NestedScrollView**
**package****import****import****import****import****import****import****import****import****import****import****import****import****import****import****import****import****import****import****import****public class****extends****implements****TVAirPressure****TVLight****TVbatteryLever****TVbatteryTemperation****private****sensorManager****lightsensorManager****private****mLight** @Override
**protected void****super**** _activity_scrolling_**** _toolbar_****TVAirPressure**** _textView2_****TVLight**** _textView4_****TVbatteryLever**** _textView6_****TVbatteryTemperation**** _textView7_**** _fab_****new** @Override
**public void** make**"Replace with your own action"****_LENGTH_LONG_****"Action"****null****lightsensorManager**** _SENSOR_SERVICE_****mLight****lightsensorManager**** _TYPE_LIGHT_****sensorManager**** _SENSOR_SERVICE_****this****this****mBatInfoReceiver****new**** _ACTION_BATTERY_CHANGED_** @Override
**public boolean** _// Inflate the menu; this adds items to the action bar if it is present.___**_menu_scrolling_****return true** @Override
**public boolean** _// 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** _//noinspection SimplifiableIfStatement_ __**if**** _action_settings_****return true****return super** @Override
**protected void** _// register this class as a listener for the Pressure Sensor_ __**lightsensorManager****this****mLight**** _SENSOR_DELAY_FASTEST_****sensorManager****this****sensorManager**** _TYPE_PRESSURE_**** _SENSOR_DELAY_NORMAL_****super** _// called when sensor value have changed_ __ @Override
**public void****if****sensor**** _TYPE_LIGHT_****float****values****TVLight****"lx "** 0**if****sensor**** _TYPE_PRESSURE_****float****values****TVAirPressure****"hpa "** 0@Override
**public void****int** @Override
**protected void****lightsensorManager****this****sensorManager****this****super****private****mBatInfoReceiver****new** @Override
**public void****int**** _EXTRA_LEVEL_** 0**TVbatteryLever****"电量级别: "** valueOf**"%"****float**** _EXTRA_TEMPERATURE_** 010**TVbatteryTemperation****"温度: "** valueOf**"°C"**
全部评论 (0)
还没有任何评论哟~
