• 传感器


     1  <TextView
     2         android:layout_width="wrap_content"
     3         android:layout_height="wrap_content"
     4         android:text="Hello World!"
     5         android:id="@+id/textView" />
     6 
     7     <Button
     8         android:layout_width="wrap_content"
     9         android:layout_height="wrap_content"
    10         android:text="手机支持的传感器"
    11         android:id="@+id/support"
    12         android:layout_below="@+id/textView"
    13         android:layout_alignParentLeft="true"
    14         android:layout_alignParentStart="true"
    15         android:onClick="click1"
    16         android:layout_marginTop="80dp" />
    17 
    18     <Button
    19         android:layout_width="wrap_content"
    20         android:layout_height="wrap_content"
    21         android:onClick="click2"
    22         android:text="震动"
    23         android:id="@+id/zd"
    24         android:layout_centerVertical="true"
    25         android:layout_alignParentLeft="true"
    26         android:layout_alignParentStart="true" />
    View Code

    2.

     1 public class MainActivity extends AppCompatActivity {
     2 
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.activity_main);
     7         init();
     8     }
     9 
    10     private void init() {
    11         //传感器管理对象
    12         SensorManager sm= (SensorManager) getSystemService(getApplicationContext().SENSOR_SERVICE);
    13         //获取加速度传感器
    14         Sensor sensor=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    15         //设置扑捉动作变换的监听器
    16         sm.registerListener(new A(),sensor,SensorManager.SENSOR_DELAY_GAME);
    17     }
    18 
    19     public  void click1(View view){
    20         SensorManager sensor= (SensorManager) getSystemService(getApplicationContext().SENSOR_SERVICE);
    21         List<Sensor> list=sensor.getSensorList(Sensor.TYPE_ALL);
    22         StringBuffer sb=new StringBuffer();
    23         for (Sensor sensor1 : list) {
    24 
    25             sb.append(sensor1.getName());
    26             System.out.println("--------"+sensor1.getName());
    27         }
    28         TextView text= (TextView) findViewById(R.id.textView);
    29         text.setText(sb.toString());
    30     }
    31     public  void click2(View view){
    32         //震动
    33         Vibrator vb= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    34         vb.vibrate(3000);
    35 
    36 
    37 
    38     }
    39 
    40     class A implements SensorEventListener {
    41 
    42         @Override
    43         public void onSensorChanged(SensorEvent event) {
    44             float []value=event.values;
    45                 if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
    46                     if (value[0] > 17 || value[1] > 17 || value[2] > 17) {
    47                         System.out.println("---" + value[0]);
    48                         //实现震动并发出声音
    49                     }
    50 
    51             }
    52 
    53         }
    54 
    55         @Override
    56         public void onAccuracyChanged(Sensor sensor, int accuracy) {
    57 
    58         }
    59     }
    60 }
    今天多一点积累,明天少一分烦恼
  • 相关阅读:
    Python记录12:迭代器+生成器+生成式
    Python记录11:叠加多个装饰器+有参装饰器
    Python记录10:模块
    Day7
    Day7
    Day7
    Day7
    Day7
    Day7
    Day7
  • 原文地址:https://www.cnblogs.com/galibujianbusana/p/5550927.html
Copyright © 2020-2023  润新知