IntelliJ Java IDE
The moose likes Android and the fly likes Android Accelerometer Sensor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Mobile » Android
Reply Bookmark "Android Accelerometer Sensor" Watch "Android Accelerometer Sensor" New topic
Author

Android Accelerometer Sensor

haythem souissi
Greenhorn

Joined: May 05, 2011
Posts: 4

Hi,

I am trying to work with Accelerometer Sensor. So i tried this
example:
http://blog.androgames.net/85/android-accelerometer-tutorial/

It work perfectly.
But when i change AccelerometerManager activity to a service, it doesn't work and i got an error.

//this is the activity that i want change public class Accelerometer extends Activity
implements AccelerometerListener {

private static Context CONTEXT;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CONTEXT = this;
}

protected void onResume() {
super.onResume();
if (AccelerometerManager.isSupported()) {
AccelerometerManager.startListening(this);
}
}

protected void onDestroy() {
super.onDestroy();
if (AccelerometerManager.isListening()) {
AccelerometerManager.stopListening();
}

}

public static Context getContext() {
return CONTEXT;
}

/**
* onShake callback
*/
public void onShake(float force) {
Toast.makeText(this, "Phone shaked : " + force, 1000).show();
}

/**
* onAccelerationChanged callback
*/
public void onAccelerationChanged(float x, float y, float z) {
((TextView) findViewById(R.id.x)).setText(String.valueOf(x));
((TextView) findViewById(R.id.y)).setText(String.valueOf(y));
((TextView) findViewById(R.id.z)).setText(String.valueOf(z));
}

}

//this is my service when i change it, my error is hir public class Accelerometer extends Service implements AccelerometerListener{ private static Context CONTEXT;

@Override
public IBinder onBind(Intent intent) {
// TODO Put your code here
return null;
}

@Override
public void onCreate() {
System.out.println(”start listening”);
// if (AccelerometerManager.isSupported()) { AccelerometerManager.startListening(this);

// }
}

@Override
public void onDestroy() {
System.out.println(”start listening”);
// if (AccelerometerManager.isListening()) { AccelerometerManager.stopListening();
// }
}

public static Context getContext() {
return CONTEXT;
}

/**
* onShake callback
*/
public void onShake(float force) {
Toast.makeText(this, “Phone shaked niktilha omha ya 3ammi el7ag: ” + force, 1000).show(); }

/**
* onAccelerationChanged callback
*/
public void onAccelerationChanged(float x, float y, float z) { System.out.println(”x = “+x+” y = “+y+” z = “+z); }

}

Thanks for help.
 
 
subject: Android Accelerometer Sensor
 
Threads others viewed
Having trouble with OnTouchListener
SharedPreference not working
Application not run in the background
Cannot find the problem in the code
android uncaught handler: thread main exiting due to uncaught exception
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture