ANDROID_MARS学习笔记_S05_006_距离传感器

 1 import android.app.Activity;
 2 import android.content.Context;
 3 import android.hardware.Sensor;
 4 import android.hardware.SensorEvent;
 5 import android.hardware.SensorEventListener;
 6 import android.hardware.SensorManager;
 7 import android.os.Bundle;
 8
 9 public class MainActivity extends Activity implements SensorEventListener {
10
11      private SensorManager mSensorManager;
12       private Sensor mProximity;
13
14       @Override
15       public final void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.main);
18
19         // Get an instance of the sensor service, and use that to get an instance of
20         // a particular sensor.
21         mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
22         mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
23       }
24
25       @Override
26       public final void onAccuracyChanged(Sensor sensor, int accuracy) {
27         // Do something here if sensor accuracy changes.
28       }
29
30       @Override
31       public final void onSensorChanged(SensorEvent event) {
32         float distance = event.values[0];
33         System.out.println("distance--->" + distance);
34         System.out.println(mProximity.getMaximumRange());
35         // Do something with this sensor data.
36       }
37
38       @Override
39       protected void onResume() {
40         // Register a listener for the sensor.
41         super.onResume();
42         mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
43       }
44
45       @Override
46       protected void onPause() {
47         // Be sure to unregister the sensor when the activity pauses.
48         super.onPause();
49         mSensorManager.unregisterListener(this);
50       }
51
52
53 }
时间: 2024-10-12 20:52:16

ANDROID_MARS学习笔记_S05_006_距离传感器的相关文章

ANDROID_MARS学习笔记_S03_007_GoogleMap1

一.简介 二.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fi

ANDROID_MARS学习笔记_S04_004_用HTTPCLENT发带参数的get和post请求

一.代码 1.xml(1)activity_main.xml 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="

ANDROID_MARS学习笔记_S02_012_ANIMATION_利用AnimationListener在动画结束时删除或添加组件

一.代码 1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/layoutId" 4 android:orientation="

ANDROID_MARS学习笔记_S01原始版_013_广播机制二

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S01原始版_012_广播机制一

一.简介 二.代码1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width

ANDROID_MARS学习笔记_S01原始版_007_Handler及线程的简单使用

一.运行结果 一.代码1.xml(1)activity_main.xml 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_heig

ANDROID_MARS学习笔记_S01原始版_008_Handler(异步消息处理机制)

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S02_004_ExpandableListActivity

1.main.xml 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"

ANDROID_MARS学习笔记_S02_005_AppWidget1

一.AppWidget介绍 1.Widget的定义创建流程 (1)在res新建xml文件夹,新建appwidget-provider.xml,为widget提供元数据 (2)在res/layout文件夹下定义widget的样式 (3)在src新建类,extends AppWidgetProvider,自定义widget (4)在AndroidManifest.xml中把自定义的widget添加为receiver,接收APPWIDGET_UPDATE广播 2. 二.代码 1.res/xml/exa