Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementing Vibration Based On Preference CheckBox; stopping when new Activity is started

 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my app I have an Activity called AlarmActivity that will start when a condition is met in MainActivity. When AlarmActivity starts, if CheckBoxPreference for vibrate is checked, I, of course, want vibration to start. I also have an onLongClickListener attached to a button that silences the alarm, so I would like to stop vibrating when the new Activity starts. Presently, vibrating starts when the AlarmActivity starts, regardless of CheckBoxPreference, and continues even after the new Activity starts onLongClick; until I exit out of the app entirely.

This is my AlarmActivity:



The key for my CheckBoxPreference is "vibrate", and the default value is set to "false".

Thanks for any help you can offer on this matter!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jared Snyder wrote:In my app I have an Activity called AlarmActivity that will start when a condition is met in MainActivity. When AlarmActivity starts, if CheckBoxPreference for vibrate is checked, I, of course, want vibration to start. I also have an onLongClickListener attached to a button that silences the alarm, so I would like to stop vibrating when the new Activity starts. Presently, vibrating starts when the AlarmActivity starts, regardless of CheckBoxPreference, and continues even after the new Activity starts onLongClick; until I exit out of the app entirely.

This is my AlarmActivity:


You call the vibrate function without ever checking the preference, so yes, if vibrates regardless of if the preference is on or off. You should read the preference and put this in an if statement dependent on the preference's value.


Does the main activity silence the vibration? If not then you need to tell the vibration to stop here.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying, Steve!

I initially added that field, because I knew I would need it, but then decided that instead of trying to implement the Vibrator call AND tie it in with the preferences that I would first implement Vibrator on its own so that I could make sure that it was working properly.

In terms of cancelling vibration, I had figured that I needed to add the cancel method to the onLongClick method, but I was fumbling over where and how to insert it. I have managed to use something like this, which is working for me



Now, I can implement the preference. However, I have never used preferences before and am a bit puzzled by how to get the current state of the CheckBoxPreference for my AlarmActivity. Any pointers?
 
Ron McLeod
Bartender
Pie
Posts: 1049
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This page gives some explanation and examples on how to use shared preferences.
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:This page gives some explanation and examples on how to use shared preferences.


So, based on the example on the Android developer site...



Call an instance of SharedPreferences named settings.
Create a boolean field named silent and set it to the value of the key silentMode, which by default is false.
Feed the value of the boolean field named silent to the method setSilent

Am I interpreting that correctly? For my purpose, on step three, would I set my boolean named alarmVibrate, and then create an if statement that references the value of alarmVibrate?
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I was on-track with this, but apparently I'm not. Regardless of if the checkbox is selected or not, no vibrate.

AlarmActivity.java:


In my preferences.xml...
 
Jared Snyder
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I ended up referring to a video on YouTube in order to complete the PreferencesActivity portion of my question...

http://www.youtube.com/watch?v=SGx03Uqn9JA

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic