tags:

views:

123

answers:

3

I will try to be as brief as possible... I have published a very simple android application it's name is "Vib-e-rator PRO". It's purpose is obvious, it can be used as a masssage vibrator or as an erotic stimulator...

My Problem is don't have an Android Phone to test my app in real time.

I have been recieving mixed comments from people. A few users say its working fine. But majority of them complained that the Phone would n quit vibrating even after the closing application.

Later i speculated that when ever the user switched off the vibrator and quit the app it would work as expected. If any user (most of em) directly closed the application ( either through the close option provided in the app itself or by pressing the END button in the phone ) without switching off the vibrator then it would not stop vibrating.

So i added myvib.Cancel() (myvib is the context for Phone Vibrator) in the exit block the close option provided in my app. For the other scenario, when it is closed by pressing the END button in phone, i have no idea how to solve it... So friends please advice me what is necessary to do... The comments i have been receiving from many users of my app is really embarassing....

+3  A: 

Check out the Activity Lifecycle. I think your best solution is to call myvib.cancel() from inside onPause() or onStop(). Then call myvib.vibrate() from onResume().

Felix
Thanks a lot Felix. Activity Life cycle is indeed the solution to my Problem. Once again, Thank you, Dude.
Mithraa
+1  A: 

On a serious note, this is something I've seen quite a lot of in my own Android apps. Of course, my code's WAY less interesting....

I can tell you from experience that the GPS listener does the same thing (and it can seriously do unkind things to the battery -- which I suppose your users have seen themselves), and I can only assume that my other listeners are doing something similar. If anybody's got a solution, I'd love to hear it, so I'll keep an eye on this thread...

I must be suffering a brain-lock, because this should be a comment, but I can't find a clicky thing to leave a comment.

As for Felix's answer, I've had very limited luck using onStop() or onPause() to shut down my threads (or is it hardware??) reliably, and I've certainly tried...

R.

Rich
You don't need a thread to activate the vibrator (the vibrator is a bit more... raw :/), thus this is not related to the question. Also, stopping a thread depends very much on how you've implemented it and what you're doing in it (I usually create a cancel() method that sets a boolean flag that I check in my thread and just exist if it's true).
Felix
I know I don't need a vibrator thread, that wasn't my point -- bad verbiage on my part. My point was ACTUALLY that it's difficult to stop quite a few Android capabilities (is that the right word?), and if anybody comes up with something even remotely foolproof, I would be very interested.
Rich
You probably want to ask this as your own question, since Felix is right that it's probably unrelated. I'll second Felix's advice all the way. The proper way to handle things that should be active whenever your activity is active and should shut down whenever it becomes inactive is in `onPause`. If you are using a background thread, you should become very comfortable with using Thread.interrupt()/Thread.isInterrupted()/InterruptedException in order to shut it down from the UI thread.
beekeeper
beekeeper: Don't read too much into my comment, I'm not experienced enough to be concerned with implementing threads, much less managing them (Even though I'm pretty sure it's easier in Java than some of my other experience). I do agree that onPause() is generally the place to manage shutting down your application, my only contribution to that was that even doing so isn't always successful (depending on what you're shutting down).
Rich
A: 

Mithraa,

You might try changing the way the user exits your app. If, instead of the End button, you implement:

onCreateOptionsMenu (Menu menu),

you can create a pop-up menu that is only displayed when the user presses the menu button (So, no ugly loss of screen real estate). The menu contents are described in an xml file, and parsed by:

onOptionsItemSelected (MenuItem item).

In my application's menu, I implement Exit to shut down my various processes gracefully. It's not universally successful, but your Vibrator control shouldn't have the issues I'm facing.

One final caveat: The pop-up menu is limited to six items (I think), and IMHO starts to get ugly once you go beyond four (A fifth menu item forces a 2nd row of menu choices and I'm not aware of anything that can be done about that.)

Here's a link to a decent example:

http://developer.android.com/guide/topics/ui/menus.html

R.

Rich
-1 Implementing an options menu does not stop the user from pressing the back (or home) button and getting frustrated because the app closed but the vibrator is still going at it.
Felix
No, it does not, but it does provide an alternative. Also, I haven't tested onKeyDown(), but couldn't it be used to essentially disable the Back and Home buttons??
Rich