80,351
社区成员
发帖
与我相关
我的任务
分享
package com.example.sensortest;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;
import android.app.Activity;
import android.hardware.SensorManager;
import android.hardware.Sensor;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity implements org.openintents.sensorsimulator.hardware.SensorEventListener {
private TextView myTextView;
private SensorManagerSimulator mySensorManager = null;//SensorManager是所有传感器的一个综合管理类,包括了传感器的种类、采样率、精准度等,用这个类的接口能取得事件的登录/删除、传感器的信息
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //加载界面
myTextView.setText("lalalalalala");
myTextView = (TextView) findViewById(R.id.mytextview);
mySensorManager = SensorManagerSimulator.getSystemService(this,SENSOR_SERVICE);//取得模拟器模拟的传感器服务
mySensorManager.connectSimulator();//连接模拟传感器的工具
//mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); //从系统服务中获得传感器管理器,通过getSystemService方法来取得一个SensorManager对象
}
@Override
protected void onResume() {
org.openintents.sensorsimulator.hardware.Sensor sensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
mySensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME ); //三个参数的意思分别为:监听器SensorListener对象,传感器的类型为光的强度 ,频率 ;其中,mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)即第二个参数是告诉系统注册光传感器
super.onResume();
}
@Override
public void onAccuracyChanged(org.openintents.sensorsimulator.hardware.Sensor arg0, int targ1) {
// TODO Auto-generated method stub
} // 当传感器的准确性更改时,将调用 onAccuracyChanged(int sensor,int accuracy)方法。参数包括两个整数:一个表示传感器,另一个表示该传感器新的准确值。
@Override
public void onSensorChanged(//此方法在传感器值更改的时候调用,函数的原型为onSensorChanged(int sensor,float?values[]),第一个参数表示要更改的传感器,第二个参数是指传感器数据本身
org.openintents.sensorsimulator.hardware.SensorEvent event) {
// TODO Auto-generated method stub
//int sensorType = event.sensor.TYPE_LIGHT;
if (event.type == Sensor.TYPE_LIGHT) {
float[] values = event.values;
myTextView.setText("当前光的强度为:"+values[0]);
// do something with the sensor data
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/mytextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/display" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sensortest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sensortest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>