Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2
This is the forum to discuss the Wrox book Professional Android Application Development by Reto Meier; ISBN: 9780470344712
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 16th, 2009, 02:42 PM
Registered User
 
Join Date: Apr 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Chapter 7 Speedometer SensorSimulator


Hi Reto Meier


Excellent book!

We're using the accelerometer simulator for a proyect:

http://code.google.com/p/openintents...ensorSimulator

The speedometer code compile fine,but the connection from the code to displaying
the updateGUI() is not working.

In the openintents code they use:

first disable the current sensor
mSensorManager.unregisterListener(mGraphView);
The View mGraphView can not be resolved??
Speedometer.java:


code
package com.paad.speedometer;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.openintents.hardware.SensorManagerSimulator;
import org.openintents.provider.Hardware;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
public class Speedometer extends Activity {
Handler handler = new Handler();
SensorManager sensorManager;
TextView myTextView;
float appliedAcceleration = 0;
float currentAcceleration = 0;
float velocity = 0;
Date lastUpdate;
View view;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

myTextView = (TextView)findViewById(R.id.myTextView);
lastUpdate = new Date(System.currentTimeMillis());
Hardware.mContentResolver=getContentResolver();
sensorManager = (SensorManager)new SensorManagerSimulator((SensorManager)getSystemSer vice(Context.SENSOR_SERVICE));
// Intent intent = new Intent(Intent.ACTION_VIEW, Hardware.Preferences.CONTENT_URI); startActivity(intent);
sensorManager.unregisterListener(sensorListener);
SensorManagerSimulator.connectSimulator();
sensorManager.registerListener(sensorListener, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_FASTEST);
Timer updateTimer = new Timer("velocityUpdate");
updateTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
updateGUI();
}
}, 0, 1000);
}
private void updateGUI() {
// Convert from meters per second to miles per hour.
final double mph = (Math.round(100*velocity / 1.6 * 3.6))/100;
// Update the GUI
handler.post(new Runnable() {
public void run() {
myTextView.setText(String.valueOf(mph) + "mph");
}
});
}
private void updateVelocity() {
// Calculate how long this acceleration has been applied.
Date timeNow = new Date(System.currentTimeMillis());
long timeDelta = timeNow.getTime()-lastUpdate.getTime();
lastUpdate.setTime(timeNow.getTime());
// Calculate the change in velocity at the
// current acceleration since the last update.
float deltaVelocity = appliedAcceleration * (timeDelta/1000);
appliedAcceleration = currentAcceleration;
// Add the velocity change to the current velocity.
velocity += deltaVelocity;
}
private final SensorListener sensorListener = new SensorListener() {
double calibration = Double.NaN;

public void onSensorChanged(int sensor, float[] values) {
double x = values[SensorManager.DATA_X];
double y = values[SensorManager.DATA_Y];
double z = values[SensorManager.DATA_Z];

double a = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));

if (calibration == Double.NaN)
calibration = a;
else {
updateVelocity();
currentAcceleration = (float)a;
}
}
public void onAccuracyChanged(int sensor, int accuracy) {}
};
}
/code
The enulator displays 0.0 mph

And sometimes(after 30 minutes) display the speed.

120 mph

How you pass the the simulator sensor values to the original sensor values?
The simulator works fine,look like it is not updated to this SDK 1.0????
Thanks.

Last edited by JHandal; April 17th, 2009 at 04:19 PM..


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 2 - End of chapter exercises whizzkid1892 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 July 30th, 2008 12:02 PM
chapter 7 - chapter 11 pelopito BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 3 January 6th, 2008 11:40 AM
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 09:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.