• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Accelerometer for Random Generator

Hi guys, I'm new here so i don't know if this is where I should make a thread.

Here's the code:

package com.android.DidYuKnow;

import java.util.Random;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class Main extends Activity implements SensorEventListener {
// For shake motion detection.
TextView window;
private SensorManager sensorMgr;
private long lastUpdate = -1;
private float x, y, z;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 500;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
// start motion detection
sensorMgr=(SensorManager) getSystemService(SENSOR_SERVICE);
sensorMgr.registerListener(this, sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
public void onAccuracyChanged(Sensor s, int arg1)
{ // TODO Auto-generated method stub
}
@Override public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
long curTime = System.currentTimeMillis();
// only allow one update every 100ms.
if ((curTime - lastUpdate) > 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
x = event.values[0];
y = event.values[1];
z = event.values[2];
float speed = Math.abs(x+y+z - last_x - last_y - last_z) / diffTime * 10000;
if (speed > SHAKE_THRESHOLD){

Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(20);
String message = "Shake to Start";
switch(randomInt) {
case 0: message = "Microsoft sends Mozilla a cup cake every time a new version of Firefox is released."; break;
case 1: message = "Snake Fish can live for 3 days out of water and can rip the flesh from your bones on land!"; break;
case 2: message = "The founder of McDonald
 
Have you tried logging to see if everything is getting called and you are getting the right result from your method to pick the text?

If that all works, try calling .requestLayout(); on your TextView and/or it's parent view.
 
There, it works! the problem was in the manifest.xml. But the problem now is that the Exit Button(our prof asked for it) is not working. I made another class for the exit.
 
Back
Top Bottom