Android_Sensor for Educational Notes of Good Knowledge

Keywords: Android Mobile Java html5

There are many kinds of sensors in mobile phones. There are a lot of sensors on the Internet. Here is a popular passage on the Internet, which is also relatively simple.
You can access these sensors and get raw data by using the Android sensor framework. Part of the Android Sensor Framework android.hardware package, which contains the following classes and interfaces:

SensorManager

You can use this class to create an instance of a sensor service. This class provides a variety of methods to access and enumerate sensors, register and cancel sensor event monitoring, and obtain the corresponding information. This class also provides several sensor constants, users report sensor accuracy, set data acquisition rates, and calibrate sensors.

Sensor

You can use this class to create an instance of the specified sensor. This class provides various methods for you to determine the function of the sensor.

SensorEvent

The system uses this class to create a sensor object that provides information about sensor events. A sensor event contains information about the original sensor data, the events generated by such sensors, the accuracy of the data, and the timestamp of the event.

SensorEventListener

You can use this interface to create two callback methods that receive notifications (sensor events) when the value of the sensor changes or when the accuracy of the sensor changes.

The following code is about the use of sensors, the use of acceleration sensors, the realization of the function is: shake the mobile phone, pop-up pictures, is the function of shake a shake, this code also has a simple animation application, seriously study.

package com.example.sj.sensordemo;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 /*
    sensor
 */
public class MainActivity extends AppCompatActivity implements SensorEventListener {
    SensorManager mSensorManager;
    private TextView mTvCenter;
    private boolean isYD;
    private ImageView goImgv;
    ObjectAnimator objectAnimator; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
        mTvCenter = (TextView) findViewById(R.id.tv_center);
        goImgv = (ImageView) findViewById(R.id.go_imgv);
        startAnim();
//        List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
//        for(Sensor sensor:sensorList){
//            System.out.println(sensor.getName());
//            
//        }
        Sensor defaultSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(this, defaultSensor, SensorManager.SENSOR_DELAY_UI);
    } 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSensorManager.unregisterListener(this);
    }
    private void startAnim() {   
        objectAnimator = ObjectAnimator.ofFloat(mTvCenter, "rotationY", -45, 45);
        objectAnimator.setDuration(3000);
        objectAnimator.setRepeatMode(ObjectAnimator.REVERSE);
        objectAnimator.setRepeatCount(-1); 
        objectAnimator.start();
    } 
    @Override
    public void onSensorChanged(SensorEvent event) {
        float[] values = event.values;
        System.out.println(values[0]);//x
        System.out.println(values[1]);//y
        System.out.println(values[2]);//z
        System.out.println("------------------------------------");  
        if (!isYD && (values[0] >= 20 || values[1] >= 20 || values[2] >= 20)) {
            isYD = true;
            Toast.makeText(this, "Shook.", Toast.LENGTH_SHORT).show();
            objectAnimator.cancel();
            goImgv.setVisibility(View.VISIBLE);
            mTvCenter.setVisibility(View.GONE);
        }
    }

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

    public void go1(View view) {
        Toast.makeText(this, "Shake one more", Toast.LENGTH_SHORT).show();
        isYD = false;
        goImgv.setVisibility(View.GONE);
        mTvCenter.setVisibility(View.VISIBLE);
        startAnim();
    } 
    public void go2(View view) {    
    }
   public void go3(View view) {     
    }
}

On the Education of Good Knowledge (Official Website: Kindness Education < Click Enter > Wechat Public Number: Good Knowledge Technology
Address: Beijing Dongyan Suburb Economic and Technological Development Zone Cultural Building
Consulting Teacher: Mr. Zheng's Telephone/Wechat: 13315631002 QQ: 1939441377
At present, we have Java servers, HTML5 front-end web pages, Android mobile terminals, PHP servers, full-time classes, weekend classes;
Tuition discount to 8480!!!!!!
Our strengths:

  • Basic courses are offered free of charge in one month.
  • Average employment salary is 10,000-15,000;
  • Free hard-of-hearing, free interdisciplinary learning;
  • 5-5.5 months of large capacity technology teaching; 0 tuition fees, 0 basic admission;
  • Massive project training, flexible teaching system, flexible arrangement of teaching time;
  • Most students find jobs with salaries above 10k a month, and the project training will be terminated naturally.
  • Video recording of the whole course, occasionally delayed and not worried;
  • Fees can be refunded at any time on the way to study without reason according to the students'conditions.
  • One subject won't be able to learn another subject for free.
  • Full-time classes, weekend classes, online teaching at the same time;
  • QQ Group of Technical Exchange: 198983438 (Where to See Our Group) In the group at any time, we will update some of our course videos and class opening dynamics.

A summary of learning videos of kindheartedness education:

Posted by shah on Sun, 09 Jun 2019 19:25:30 -0700