[TinyTut] - Android VibratorService

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[TinyTut] - Android VibratorService

Postby plusminus » Fri Dec 14, 2007 5:24 pm

[TinyTut] - Android VibratorService ;)


What you learn: You will learn how to use Androids VibratorService.

Difficulty: 1 of 5 :)

:idea: Questions/Problems: Simply post below...

Description:
Vibration is also as easy as the most things on the Android-Platform :!:
What we need to do are 2 simple things:
  1. Get the Service, that is responsible for Vibration.
  2. Call myVibratorService.vibrate(int) .
So, vibration can be done within two lines of code :!:

Sample Activity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class AndroidVibrator extends Activity {
  2.         android.os.IVibratorService myVibratorService = null;
  3.  
  4.         /** Called when the activity is first created. */
  5.         @Override
  6.         public void onCreate(Bundle icicle) {
  7.                 super.onCreate(icicle);
  8.                 setContentView(R.layout.main);
  9.  
  10.                 // Receive the VibratorManagerService
  11.                 myVibratorService = android.os.IVibratorService.Stub
  12.                                 .asInterface(ServiceManager.getService("vibrator"));
  13.  
  14.                 // Do a short (100ms) vibration.
  15.                 try {
  16.                         myVibratorService.vibrate(100);
  17.                 } catch (DeadObjectException e) { }
  18.         }
  19. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


For continuous vibration you could write the following:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                                 new Thread(){
  2.                                         @Override
  3.                                         public void run(){
  4.                                                 try {
  5.                                                         while(!isInterrupted()){
  6.                                                                 myVibratorService.vibrate(500);
  7.                                                                 Thread.sleep(1000);
  8.                                                         }
  9.                                                 } catch (Exception e) { }
  10.                                         }
  11.                                 }.start();
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

Do not forget to interrupt the Thread somewhen ;)

This is the output you can see in the LogCat:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. I/Vibrator(484): off
  2. I/Vibrator(484): on
  3. I/Vibrator(484): off
  4. I/Vibrator(484): on
  5. ...
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Last edited by plusminus on Fri Dec 28, 2007 2:09 pm, edited 1 time in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby lordhong » Sun Dec 16, 2007 9:37 pm

:shock: i was doing the same thing today after looking at davanum's blog on system service... and find this article in google... i should check anddev more often or is it just more threads than i can handle? :oops: this is good stuff...
lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

Postby plusminus » Sun Dec 16, 2007 10:22 pm

Hey lordhong,

hehe, we are posting faster than light :roll:
Doing best as we can :)

I'm currently working on the SMS-Compressor. (Unimproved standard Huffman-Compression I coded is working at awesome 40-60% size of original message :!: )
You'll find sth here soon :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby lordhong » Sun Dec 16, 2007 10:38 pm

:( i wish i can quit my day job and doing Android stuff all day long... daily work sux big time...

:roll: oh well, very anxious to see your SMS compressor :wink:
lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

Postby plusminus » Sun Dec 16, 2007 11:04 pm

I can show you some JUnit-Output by now from the Compression by now:.

The Huffman-Compression is a lossless compression.
The compression will be even better, if we allow just lowercase input (the case could be basically restored by logic)
I picked a random String from Wikipedia to setup the 'Dictionary'.

This is the output:
Code: Select all
TestInputString: 'der vom shannon-fano-algorithmus erseugte baum ist nicht immer optimal benutse dabei die summe der wahrscheinlichkeiten der unterbauume'
Bits used with no compression: 1080

BinaryEncoded: '10101000000010001100101101110010000110000001101110
1110111101101110110010010101000111011110110110010010111011010001
1110110001001000101000110100100110111000001000000010100000001101
1000111010100000010110001111101110010000101001000010100101110100
1100000110010100101001001001001000000000100011011100110101010010
0100111011010010110000000011111011010110000000001101011110110000
0000100001101010100000000110001101110010010010000000011010100000
0010001110100111000110000101000110000011000000100011101101010011
0000011001100101000001000101000001110011010100000001000111011011
1010100000001001100011111011110111001000000'

Bits used after compression: 605
Ratio: 56%
Back-Decoded: 'der vom shannon-fano-algorithmus erseugte baum ist nicht immer optimal benutse dabei die summe der wahrscheinlichkeiten der unterbauume'


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sbfalbo » Wed Dec 26, 2007 7:19 pm

Hello,
Does anyone know how to use Android to turn off the phone's ringer and place it in Vibrate only mode?
Thanks!
sbfalbo
Freshman
Freshman
 
Posts: 8
Joined: Wed Dec 26, 2007 7:17 pm

Top

Postby rtreffer » Wed Dec 26, 2007 9:22 pm

plusminus wrote:I'm currently working on the SMS-Compressor. (Unimproved standard Huffman-Compression I coded is working at awesome 40-60% size of original message :!: )
You'll find sth here soon :)


Why don't you use any kind of repetition elimination? Something like BWT/MTF/RLE (bad idea, sms is too short) or LZ__ (e.g. LZ77, LZ78, LZW)? Is your huffman static + optimal (=stored in front of the msg), static (precomputed based on a long english or german text) or adaptive (quite expensive)? Have you considered Markov chains (or even PPM(X))? Or Arithmetic Coding as an alternative to Huffman?
root@localhost# : ( ) { : | : & } ; :
rtreffer
Junior Developer
Junior Developer
 
Posts: 15
Joined: Fri Nov 23, 2007 4:14 pm

Postby plusminus » Thu Dec 27, 2007 12:15 am

Hello rtreffer,

By now I'm using static (with a random pretty short text, just for some unit-testing by now :!:), because sending a custom dictionary within just 160*7 Bits is probably less optimal than some precomputed ones.
I thought of some extra dictionary replacement, aka "i love you" :arrow: "zy" (as "zy" does not appear in normal textmessages) and also of some dictionaries with specialized Context (like PC-Stuff, or Love-Issues).

By now the project is a bit stuck, because SMS does not yet get saved within the emulator (we can receive but the do not get stored, wohoo :roll: ). I though of recognizing the message by one or two bytes (one in the front, and one in the end(checksum perhaps)) then storing them, to the common SMS-Storage(if there would already be one ^^). Saving them to our own DataBase would be done quick.

Unfortunately my time is running out in January (holiday + exams :( )... we will see what I can release so far :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Katharnavas » Thu Dec 27, 2007 6:34 am

Hi,
How i could find that the vibrator is working in emulator..
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby lordhong » Thu Dec 27, 2007 2:01 pm

you can use Log to log the vibration, also there are some system messages u can find in logcat about vibration on/off etc :wink:
lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

Postby Katharnavas » Thu Dec 27, 2007 2:24 pm

lordhong wrote:you can use Log to log the vibration, also there are some system messages u can find in logcat about vibration on/off etc :wink:


Only logs we are able to find.. Doesn't any hardware visualizations that emulator is shaking etc.. if so it will be pretty good than finding out in the logs.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby plusminus » Fri Dec 28, 2007 2:11 pm

:D Katharnavas,

I had exactly the same thought :lol: Would be a really nice feature.

Btw the LogCat out put looks liek the following:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. I/Vibrator(484): off
  2.  
  3. I/Vibrator(484): on
  4.  
  5. I/Vibrator(484): off
  6.  
  7. I/Vibrator(484): on
  8.  
  9. ...
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Jesmiatka » Fri Feb 08, 2008 12:15 pm

Hi,

I am using the vibration of the notifcation and I get some weird results in my logcat.

Defined long array for the lengths wich I fill in the onCreate.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.        
  2.  
  3. private long[] vibratePattern = new long[3];
  4.  
  5. private Notification notification;
  6.  
  7.  
  8.  
  9. vibratePattern[0] = 1000;
  10.  
  11. vibratePattern[1] = 1000;
  12.  
  13. vibratePattern[2] = 1000;
  14.  
  15.  
  16.  
  17. notification = new Notification(R.drawable.icon, "Notification", getIntent(), null, null);
  18.  
  19. notification.vibrate = vibratePattern;
  20.  
  21.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


and now my logcat tells me the following about the vibrating:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. I/Vibrator(635): off
  3.  
  4. I/Vibrator(635): on
  5.  
  6. I/Vibrator(635): off
  7.  
  8. I/Vibrator(635): off
  9.  
  10.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Am I using something i should not be using or is it reacting in a different way I think it should? ;-)
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby plusminus » Fri Feb 08, 2008 2:55 pm

Hello jesmiatka,

I found this in the google-samples :darrow:. The first long in the array seems to be the "offset", so your is perhaps :roll: right and more is logged than it should ^^.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         // after a 100ms delay, vibrate for 250ms, pause for 100 ms and
  2.         // then vibrate for 500ms.
  3.         notif.vibrate = new long[] { 100, 250, 100, 500 };
  4.  
  5.  
  6.         // send the notification ============================================
  7.  
  8.         nm.notifyWithView(R.layout.incoming_message_panel, view,
  9.                 NotificationManager.LENGTH_LONG, notif);
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Jesmiatka » Fri Feb 08, 2008 3:16 pm

Seems to be it!
I changed my array, added 1 additional value.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private long[] vibratePattern = new long[] { 100, 500, 1000, 1500 };
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Got the following log now:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. I/Vibrator(465): off
  2.  
  3. I/Vibrator(465): on
  4.  
  5. I/Vibrator(465): off
  6.  
  7. I/Vibrator(465): on
  8.  
  9. I/Vibrator(465): off
  10.  
  11.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: Yahoo [Bot] and 8 guests