package com.example.codeprojectandroid.accelerometersample;
import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class AccelerometerActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private long lastUpdate;
private int color = Color.RED;
RelativeLayout relativeLayout;
TextView textViewX;
TextView textViewY;
TextView textViewZ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accelerometer);
relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout1);
textViewX = (TextView)findViewById(R.id.textViewX);
textViewY = (TextView)findViewById(R.id.textViewY);
textViewZ = (TextView)findViewById(R.id.textViewZ);
// Get the SensorManager instance
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
lastUpdate = System.currentTimeMillis();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.accelerometer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged (SensorEvent event) {
DisplayAccelerometerReadings(event);
CheckShake(event);
}
private void DisplayAccelerometerReadings(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
textViewX.setText("X axis reading: " + x);
textViewY.setText("Y axis reading: " + y);
textViewZ.setText("Z axis reading: " + z);
}
private void CheckShake(SensorEvent event) {
// Movement
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
float accelerationSquareRoot = (x * x + y * y + z * z)
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
long actualTime = System.currentTimeMillis();
if (accelerationSquareRoot >= 2) //
{
if (actualTime - lastUpdate < 200) {
return;
}
lastUpdate = actualTime;
if (color == Color.RED) {
relativeLayout.setBackgroundColor(Color.YELLOW);
color = Color.YELLOW;
} else {
relativeLayout.setBackgroundColor(Color.RED);
color = Color.RED;
}
}
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Android Sensor 示例demo (506个子文件)
app-debug.ap_ 51KB
app-debug.ap_ 50KB
app-debug.ap_ 50KB
app-debug.ap_ 50KB
app-debug.ap_ 50KB
app-debug.ap_ 50KB
app-debug.ap_ 50KB
app-debug.ap_ 50KB
app-debug.apk 56KB
app-debug-unaligned.apk 56KB
app-debug.apk 56KB
app-debug-unaligned.apk 56KB
app-debug.apk 56KB
app-debug-unaligned.apk 56KB
app-debug.apk 56KB
app-debug-unaligned.apk 56KB
app-debug.apk 55KB
app-debug-unaligned.apk 55KB
app-debug.apk 55KB
app-debug-unaligned.apk 55KB
app-debug.apk 55KB
app-debug-unaligned.apk 55KB
app-debug.apk 55KB
app-debug-unaligned.apk 55KB
gradlew.bat 2KB
gradlew.bat 2KB
gradlew.bat 2KB
gradlew.bat 2KB
gradlew.bat 2KB
gradlew.bat 2KB
gradlew.bat 2KB
gradlew.bat 2KB
fileSnapshots.bin 69KB
fileSnapshots.bin 65KB
fileSnapshots.bin 65KB
fileSnapshots.bin 65KB
fileSnapshots.bin 55KB
fileSnapshots.bin 55KB
model_data.bin 46KB
model_data.bin 45KB
model_data.bin 45KB
model_data.bin 45KB
fileSnapshots.bin 45KB
model_data.bin 45KB
model_data.bin 45KB
model_data.bin 45KB
model_data.bin 44KB
fileSnapshots.bin 40KB
taskArtifacts.bin 39KB
taskArtifacts.bin 39KB
taskArtifacts.bin 39KB
taskArtifacts.bin 39KB
taskArtifacts.bin 38KB
taskArtifacts.bin 38KB
taskArtifacts.bin 34KB
taskArtifacts.bin 29KB
fileHashes.bin 21KB
fileHashes.bin 21KB
fileHashes.bin 21KB
fileHashes.bin 21KB
fileHashes.bin 21KB
fileHashes.bin 21KB
fileHashes.bin 20KB
fileHashes.bin 20KB
outputFileStates.bin 19KB
outputFileStates.bin 19KB
outputFileStates.bin 19KB
outputFileStates.bin 19KB
outputFileStates.bin 19KB
outputFileStates.bin 19KB
outputFileStates.bin 19KB
outputFileStates.bin 19KB
AccelerometerActivity.class 4KB
EnvironmentalSensorActivity.class 3KB
GravitySensorActivity.class 3KB
OrientationSensorActivity.class 3KB
GyroScopeActivity.class 3KB
GeoMagneticSensorActivity.class 3KB
ProximitySensorActivity.class 3KB
GSPActivity.class 2KB
CustomLocationListener.class 2KB
R.class 1KB
R.class 1KB
R.class 1KB
R.class 1017B
R.class 997B
R.class 997B
R.class 957B
R.class 867B
BuildConfig.class 826B
BuildConfig.class 817B
BuildConfig.class 817B
BuildConfig.class 811B
BuildConfig.class 805B
BuildConfig.class 805B
BuildConfig.class 793B
BuildConfig.class 766B
R$id.class 760B
R$id.class 690B
R$id.class 672B
共 506 条
- 1
- 2
- 3
- 4
- 5
- 6
aikongmeng
- 粉丝: 1182
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页