• 温度传感器,摇一摇


    权限: <uses-permission android:name="android.permission.VIBRATE" />



    布局:


    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.my_chuanganqi_yaoyiyao.MainActivity">

    <TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

    <TextView
    android:id="@+id/tv_1"
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="摇一摇 开始啦~\(≧▽≦)/~"
    android:gravity="center"
    android:textSize="40sp"
    android:textColor="#ffffff"
    android:background="#000000"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

    <TextView
    android:id="@+id/tv_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="温度"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="100dp" />
    </RelativeLayout>








    package com.example.my_chuanganqi_yaoyiyao;

    import android.content.Context;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.media.MediaPlayer;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;

    import java.util.List;

    public class MainActivity extends AppCompatActivity {

    private TextView tv;
    private TextView tv_1; // 显示摇一摇
    private TextView tv_2; // 显示温度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (TextView) findViewById(R.id.tv);
    tv_1 = (TextView) findViewById(R.id.tv_1); // 显示摇一摇
    tv_2 = (TextView) findViewById(R.id.tv_2); // 显示温度

    // 获取 系统传感器管理器
    SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    // 通过传感器管理器 获取 本地所有的传感器
    List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);

    for (Sensor s: sensors) {
    System.out.println("Sensor == " + s.toString());
    }

    // 获取指定的某一个传感器
    Sensor type_accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    if (type_accelerometer!=null) {
    System.out.println("Sensor 获取指定的某一个传感器 " + type_accelerometer.toString());
    }

    // 注册传感器的监听器 (摇一摇)
    sm.registerListener(new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
    // 传感器数据变化,在该方法中我们可以获取传感器变化的值
    float x = sensorEvent.values[0];
    float y = sensorEvent.values[1];
    float z = sensorEvent.values[2];
    tv.setText("x="+x+",y="+y+",z="+z);

    if (Math.abs(x)+Math.abs(y)+Math.abs(z)>=ringValue && flag == false){
    flag = true;
    tv_1.setVisibility(View.VISIBLE);
    MediaPlayer mp = MediaPlayer.create(MainActivity.this,R.raw.yaoyiyao);
    mp.start();
    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
    mediaPlayer.release();
    flag = false;
    MediaPlayer.create(MainActivity.this,R.raw.yaodaole).start();
    tv_1.setVisibility(View.GONE);
    }
    });
    }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {
    // 传感器精度的变化
    }
    },type_accelerometer,SensorManager.SENSOR_DELAY_NORMAL);



    // 温度传感器
    Sensor type_ambient_temperature = sm.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
    System.out.println(type_ambient_temperature);
    sm.registerListener(new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
    float temp = sensorEvent.values[0];
    System.out.println(sensorEvent.values[0]); System.out.println(sensorEvent.values[1]); System.out.println(sensorEvent.values[2]); temp = (float) (Math.round(temp * 10.0) / 10.0); // 四舍五入 保留一位小数 tv_2.setText("温度:" + temp + "°C"); } @Override public void onAccuracyChanged(Sensor sensor, int i) { System.out.println(sensor); System.out.println(i); } },type_ambient_temperature, SensorManager.SENSOR_DELAY_NORMAL); } int ringValue = 40; boolean flag = false;}
  • 相关阅读:
    面向对象一
    模块二:os模块、sys模块、json模块、pickle模块,包
    模块一:时间模块、random模块、hashlib模块、日志模块
    异常处理、迭代器、推导式、生成器
    有参装饰器
    匿名函数、高阶函数
    装饰器
    函数对象、函数嵌套、闭包函数
    名称空间与作用域
    day17 django 相关
  • 原文地址:https://www.cnblogs.com/98k98k/p/7822545.html
Copyright © 2020-2023  润新知