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 com.example.*******.sensor;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class ScrollingActivity extends AppCompatActivity  implements SensorEventListener{
    TextView TVAirPressure,TVLight,TVbatteryLever,TVbatteryTemperation;
    private SensorManager sensorManager,lightsensorManager;
    private Sensor mLight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TVAirPressure=(TextView)findViewById(R.id.textView2);
        TVLight=(TextView)findViewById(R.id.textView4);
        TVbatteryLever=(TextView)findViewById(R.id.textView6);
        TVbatteryTemperation=(TextView)findViewById(R.id.textView7);

        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

            }
        });

        lightsensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mLight = lightsensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

        sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
        this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));






    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_scrolling, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // 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 id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



    @Override
    protected void onResume() {

        // register this class as a listener for the Pressure Sensor
        lightsensorManager.registerListener(this, mLight,
                SensorManager.SENSOR_DELAY_FASTEST);


        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),
                SensorManager.SENSOR_DELAY_NORMAL);


        super.onResume();


    }
    // called when sensor value have changed
    @Override
    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
            float[] values = event.values;
            TVLight.setText("lx  "+values[0]);
        }

        if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
            float[] values = event.values;
            TVAirPressure.setText("hpa  "+values[0]);
        }



    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    protected void onPause() {
        lightsensorManager.unregisterListener(this);

        sensorManager.unregisterListener(this);
        super.onPause();

    }

    private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context ctxt, Intent intent) {
            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            TVbatteryLever.setText("电量级别: "+String.valueOf(level) + "%");

            float temperation=intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0)/10;   这里要除10,不信你可以试试
            TVbatteryTemperation.setText("温度: "+String.valueOf(temperation)+"°C");
        }
    };


}
 
  
 
  

你可能感兴趣的:(Android动态获取传感器数据)