last day (14 days later) » 

5:47 PM
Hello
So here's my code in my service:
 
First I would clean up your ContentObserver to this:
public class VolumeCheck extends ContentObserver {
int previousVolume;
Context context;
AudioManager audioManager;

public VolumeCheck(Context c, Handler handler) {
super(handler); //Creates a new handler
context=c; //variable context, defined earlier, is set equal to c, context of service.

audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); //retrieve an AudioManager for handling management of volume, ringer modes and audio routing.
previousVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); //The volume that we get before the `onChange` is called
 
What changed?
 
and don't forget to ad previousVolume = currentVolume; in the processVolume change method.
I kept a reference to the audiomanager instead of getting it every time.
and removed the unnecessary previousVolume parameter to processVolumeChange
 
Why do I need to add previousVlume = currentvolume
 
Don't you want to detect changes in volume?
after the volume changes you need to reset previous volume.
 
5:51 PM
Oh, we need to reset it for next time?
So is it like this then?
  public void processVolumeChange(int currentVolume) {
        MyService mService = new MyService();
        mService.volumeCheck(previousVolume - currentVolume); //Method in my service

        previousVolume = currentVolume; //Set previous volume equal to current volume for next time
    }
 
right
so if you update volume from 2->4 and then later 4->8 the difference will be 4 instead of 6
Are you getting different values now when you change the volume control in the image?
 
I thought that the previousVolume = currentVolume would be unneccessary because it will already get set next time in the volumeCheck method, right?
 
That code only runs once when you first create the VolumeCheck object.
not each time you change the volume.
comment out the call to your service so you can test the volume change now
 
Oh okay
 
since the service is what isn't working.
 
5:56 PM
public void volumeCheck(int difference) {

Toast.makeText(MyService.this, "Changed volume this much: " +difference, Toast.LENGTH_SHORT).show();

}
that is the code in my service
 
don't use your service yet.
see if what I suggested fixes your original question
 
Okay so let me comment out service and debug
so this is where I should place the break points, right?
just making sure:
 
That's fine
then you can see both current and previous values
actually do one line down
after currentVolume is assigned
 
Where
 
processVolumeChanged(currentVolume) is fine
 
6:00 PM
But then how will I now the previous volume?
 
Because it is a variable on the class
you can just mouse over it even to see current value
 
But then what about previous volume/
 
mouse over it
it's a field on the class
 
They still equal the same thing
Oh wait i forgot to comment out the service line
So like this:
public void processVolumeChange(int currentVolume) {
// MyService mService = new MyService();
//mService.volumeCheck(previousVolume - currentVolume); //Method in my service

previousVolume = currentVolume; //Set previous volume equal to current volume for next time
}
 
You can even put the breakpoint on the line previousVolume = currentVolume;
that will make it real easy
 
6:08 PM
Okay
 
I get that the two are the same
in the line
previousvolume=curentvolume
When I click on previous volume
 
did you change the media volume as I showed in the image?
 
This is so anyouing, the debug keeps stopping
 
You might only be changing the ringer volume otherwise
 
6:14 PM
 
The bottom one
 
So I need to change the music, video, games and other media one
 
Okay, on my LG-G3 its showing different values which is good
but on my HUAWEI
I think it wasnt working let me check again
Also, why wasn't the toast showing?
Can I change it to rington volume
Because I want it to be the ringtone volume that I am checking
 
MyService mService = new MyService(); if that extends service you shouldn't create your own service that way.
yes go ahead and change it to ringtone volume then
 
6:19 PM
Okay I changed it to STREAM_RING
Wha were you saying aboug the service object
my service is already creatd
I was doing that just to call the method
So it is working now
But the toast isnt showing
 
That would be another question.
MyService mService = new MyService(); is not correct
 
Then how should I call my method
I just want to call my method volumeCheck from my listener class
where method volumeCheck is in my service
 
easiest way to get you started would be to just make that method static on the service.
 
then I cant display the toast
 
Do you really need a toast?
 
6:25 PM
Nevermidn then
Is there anyway
to make that listener only respond to ringotne changes
right now it is responding to all media changed
 
Did you try STREAM_RING?
 
Yeah I did that for my variables current volume and previous voleume
but do I need to do that for my audiomanager
    audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); //retrieve an AudioManager for handling management of volume, ringer modes and audio routing.
 
Just do audioManager.getStreamVolume(AudioManager.STREAM_RING); when you get the volume instead.
 
I did that
 
ok so it should be what you want
I tested it myself.
 
6:31 PM
But I'm asking how I can make the onChange method only get called when I change the ringer, rather than any media volume
 
That also is another question. At least you can check if the volume you are interested in changed by seeing if the value changed.
If I knew of a way to do it offhand I would let you know but I don't
 
Okay
The reason I'm asking is because I just want to see if the user has held down the volume button
So if the volume is already at 0
and they hold down the down volume button
it will still be at 0
 
You can still check if currentValue == 0 then
 
What do you mean
I will need something like if(current volume == 0 && previousvolume==0){}
But that may be triggered if I change my media volume also
and my volume is als o at 0
 
6:46 PM
Why do you care if they don't change the volume level?
what are you trying to do with the volume level?
 
I just want to see whether they hold down the volume button
 
You might want to search if there is a better way to do that then
anyways I answered the question in the post so good luck with everything else!
 
Okay, I have accepted your answer and liked it
And although you answered my question, do you mind just explaining this? How would I call the service's method when it is not static
Because I can't do a lot of things when it is static
 
7:04 PM
@GeorgeMulligan Fore example if I want to vibrate
 

last day (14 days later) »