Vibrator
Monkey Targets Forums/Android/Vibrator
| ||
Is it possible to extend monkey so it can control the phones vibrator? I tryed cooking some code myself, but it wont compile :( I would appreciate if someone could have a look and spot any obvious mistakes... vibandroid.java // VibAndroid.java import android.content.Context; import android.os.Vibrator; class VibAndroid { static void Vibrate() { // See the docs for android.os.Vibrator for more info about the format of this array long SPEED_BASE = 100; long[] pattern = new long[] { SPEED_BASE, SPEED_BASE, SPEED_BASE }; // Start the vibration Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(SPEED_BASE, -1); } } testvib.monkey Import mojo Import "VibAndroid.java" Extern Function Vibrate:Void()="VibAndroid.Vibrate" Public Class Game Extends App Method OnCreate() SetUpdateRate 60 End Method OnRender() Cls 30,30,30 DrawText "TOUCH TO VIBRATE",0,0 If MouseDown(0) Then Vibrate() End End Function Main() New Game End |
| ||
Hi, I was just looking at this because I fancied using the same function. class VibAndroid { static void Vibrate() { // Start the vibration android.os.Vibrator vibrator = (Vibrator) MonkeyGame.activity.getSystemService(android.content.Context.VIBRATOR_SERVICE); vibrator.vibrate(50); } } Try swapping your vibandroid.java file to the above, and it should work. Also I forgot to mention you will need to add the following permission in your manifest file : <uses-permission android:name="android.permission.VIBRATE" /> |
| ||
ah, I was just writing a feature for monkey to use the vibrator ;o), it's not completely finished as I wanted to add a bit more, but this is what I have so far. If anyone can help me improve the code as I'm fairly new to Java myself, please do, so I can learn from it :o) Happy to help! monkeyvibrate.java import android.os.Vibrator; import android.content.Context; class monkeyvibrate { public static Vibrator vibrator; public static void StartVibrate(int millisec) { vibrator = (Vibrator)MonkeyGame.activity.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(millisec); } public static void StopVibrate() { vibrator.cancel(); } } monkeyvibrate.monkey Import mojo Import "monkeyvibrate.java" Extern Function StartVibrate:Void(millisec : Int) = "monkeyvibrate.StartVibrate" Function StopVibrate:Void() = "monkeyvibrate.StopVibrate" Public MonkeyVibrateTest.monkey Import mojo.app Import mojo.graphics Import mojo.input Import monkeyvibrate Class MonkeyVibrateTest Extends App Method OnCreate() SetUpdateRate 30 End Method OnUpdate() If TouchHit(0) Then StartVibrate(1000) Endif End Method OnRender() End End Class Function Main() New MonkeyVibrateTest End Function as stated by Uncle, don't forget the <uses-permission android:name="android.permission.VIBRATE" /> in the manifest file. |
| ||
Cool... Thanks for this, Im sure I tried this earlier and it didnt work. We've slightly altered it and adding it to Diddy: public static Vibrator vibrator; public static void startVibrate(int millisec) { try { vibrator = (Vibrator)MonkeyGame.activity.getSystemService(Context.VIBRATOR_SERVICE); if (vibrator!=null) vibrator.vibrate(millisec); } catch (java.lang.SecurityException e) { android.util.Log.e("[Monkey]", "SecurityException: " + android.util.Log.getStackTraceString(e)); } } public static void stopVibrate() { try { if (vibrator!=null) vibrator.cancel(); } catch (java.lang.SecurityException e) { android.util.Log.e("[Monkey]", "SecurityException: " + android.util.Log.getStackTraceString(e)); } } So now it wont display the permissions warning and it wont crash if you call StopVibrate before StartVibrate :) |
| ||
ah nice addition, I will commit your changes to memory ;) thanks! |
| ||
Is there any way to adjust strength of vibration? [EDIT] I found the answer :) http://groups.google.com/group/android-developers/browse_thread/thread/10bcd0036c42f418 |
| ||
i have HTC Desire Z and i can feel that vibration is comming from particular place from the phone (left side, top side etc) including strength of the vibro. Can we do something similar here ? This really gives you great feeling of playing the "real" device not just the phone. I think this might be related to device not to the software. |
| ||
I am trying to implement vibration in my app. So I created the files as stated by Teaboy, but when building the MonkeyVibrateTest.monkey app, I get this:-pre-compile: -compile: [javac] Compiling 3 source files to D:\Programming\Projects\vibrate\MonkeyVibrateTest.build\android\bin\classes [javac] D:\Programming\Projects\vibrate\MonkeyVibrateTest.build\android\src\com\monkey\MonkeyGame.java:2581: error: cannot find symbol [javac] vibrator = (Vibrator)MonkeyGame.activity.getSystemService(Context.VIBRATOR_SERVICE); [javac] ^ [javac] symbol: variable activity [javac] location: class MonkeyGame [javac] Note: D:\Programming\Projects\vibrate\MonkeyVibrateTest.build\android\src\com\monkey\MonkeyGame.java uses unchecked or unsafe operations. [javac] Note: Recompile with -Xlint:unchecked for details. [javac] 1 error BUILD FAILED d:\android-sdk\tools\ant\build.xml:710: The following error occurred while executing this line: d:\android-sdk\tools\ant\build.xml:723: Compile failed; see the compiler error output for details. Total time: 2 secondsTRANS FAILED: Android build failed. Abnormal program termination. Exit code: -1 I have no clue what to do... |
| ||
That's because Monkey has changed quite a bit since these posts were made. Check out how I updated Diddy to make it work with the new version: http://code.google.com/p/diddy/source/browse/src/diddy/native/diddy.android.java Basically, MonkeyGame.activity is no more and you have to use BBAndroidGame._androidGame._activity instead. |
| ||
Thank you Therevills. Got it working now! |