by Sapientum » Wed Dec 02, 2009 11:38 am
hey guys, I'm trying to write some code that makes the phone vibrate and for testing purpose, I have made it so that when I press the volume up/down buttons it should vibrate, but the application is forced close, when I press a button.
My code is as following:
package example.Volume;
import android.app.Activity;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.content.Context;
import android.content.Intent;
public class Volume extends Activity {
private Vibrator vibrator;
int milliseconds = 500;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
//Creates a function that listen for whether a key is pressed
public boolean onKeyDown(int keyCode, KeyEvent ev) {//int _keyCode, KeyEvent _event)
switch(keyCode){
//Creates a case for each of the keys to listen to
case KeyEvent.KEYCODE_VOLUME_DOWN:
//Tells the application what to do when VOLUME_DOWN is pressed
vibrator.vibrate(milliseconds);
break;
case KeyEvent.KEYCODE_VOLUME_UP:
vibrator.vibrate(milliseconds);
break;
default:
return false;
}
return true;
}
}