tags:

views:

58

answers:

1

Hello. I'm trying to determine the current ringer state by using the getRingerMode() method.

I create a variable: private int currentRingerMode;

Set my variable to: currentRingerMode = getRingerMode();

And use a methods as follows:
private int getRingerMode() { // TODO Auto-generated method stub return currentRingerMode; }

But when I log the value of the variable with: Log.v(TAG, "value=" + currentRingerMode);

A value of 0 is always returned, regardless of the the ringer state...??? What am I doing wrong?

Thanks.

-Brian


-Update Here is my current full code.

package com.DoNotDisturb.widget;

import com.DoNotDisturb.widget.R;

import android.app.PendingIntent; import android.app.Service; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; import android.media.AudioManager; import android.util.Log; import android.widget.RemoteViews; import android.widget.Toast;

public class DoNotDisturbWidget extends AppWidgetProvider {

private static final String ACTION_WIDGET_RECEIVER = "ActionRecieverWidget";
private static final String TAG = null;
private int currentRingerMode;
private int ringerMode;
private RemoteViews views = new RemoteViews("com.DoNotDisturb.widget", R.layout.main);  

private AudioManager getSystemService(String audioService) {
    // TODO Auto-generated method stub

    return null;
    }


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    Intent intent = new Intent(context, DoNotDisturbWidget.class);
    intent.setAction(ACTION_WIDGET_RECEIVER);


    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.button_one, pendingIntent);

    appWidgetManager.updateAppWidget(appWidgetIds, views);

    AudioManager mAudioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE); 
    int ringerMode = mAudioManager.getRingerMode();
    Log.d(TAG, "ringerMode value=" + ringerMode);

    ContextWrapper mContext = null;
    AudioManager manager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    currentRingerMode = manager.getRingerMode();
    Log.d(TAG, "ringerMode value=" + currentRingerMode);

}   



@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)){

        Log.d(TAG, "ringerMode value=" + ringerMode);

        try {

            //Silent
            if(currentRingerMode == 2){
            Toast.makeText(context, "Silent", Toast.LENGTH_SHORT).show();
            ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).setRingerMode(AudioManager.RINGER_MODE_SILENT);
            Log.v(TAG, "After ButtonPress value=" + currentRingerMode);


            }

            //Vibrate
                if(currentRingerMode == 1){
                Toast.makeText(context, "Normal", Toast.LENGTH_SHORT).show();
                ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                Log.v(TAG, "After ButtonPress value=" + currentRingerMode); 


                }

            //Normal Mode
            if(currentRingerMode == 0){
                Toast.makeText(context, "Silent", Toast.LENGTH_SHORT).show();
                ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).setRingerMode(AudioManager.RINGER_MODE_SILENT);
                Log.v(TAG, "After ButtonPress value=" + currentRingerMode); 

            }
        }

        finally{}

     }

    super.onReceive(context, intent);
}

}

A: 

When you declare private int currentRingerMode;, the default value of currentRingerMode is 0.

And since you are calling getRingerMode() , apparently not from an AudioManager object, your method

private int getRingerMode() { 
   // TODO Auto-generated method stub return currentRingerMode; 
   return currentRingerMode;
}

is called, hence the 0 all the time

UNTESTED

So first, you have to retrieve the service for Audio

AudioManager manager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

And from it, call the getRingerMode()

currentRingerMode = manager.getRingerMode();

EDIT If you are inside an activity , the following should work

private int currentRingerMode;
AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
currentRingerMode = manager.getRingerMode();

EDIT 2

In your onUpdate, try this

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    Intent intent = new Intent(context, DoNotDisturbWidget.class);
    intent.setAction(ACTION_WIDGET_RECEIVER);


    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.button_one, pendingIntent);

    appWidgetManager.updateAppWidget(appWidgetIds, views);

    AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    currentRingerMode = manager.getRingerMode();
    Log.d(TAG, "ringerMode value=" + currentRingerMode);

} 

Also , look at the LogCat and make sure you have the permission on AUDIO.

ccheneson
Also don't forget to set the required permissions to access the Audio service
ccheneson
Thanks for the response.So if I understand it correctly, I still declare my variable in the same way like I did before, but my code should look like this?:ContextWrapper mContext = null; AudioManager manager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); currentRingerMode = manager.getRingerMode(); Log.d(TAG, "ringerMode value=" + currentRingerMode);Thanks.
Brian Fleishman
You are calling getSystemService from a mContext that is null (And I dont know the difference between ContextWrapper and a Context). You have to call it from a valid Context (for example an activity). Beside this, the rest is correct.
ccheneson
ok. So I guess I should be replacing mContext with a parameter that is not NULL? The strange thing is that this exact code works fine inside a regular application that I developed which uses a buttonReciever. But this is a widget using a pendingIntent which I guess makes a difference? Should I have to change anything because of that?
Brian Fleishman
If by regular application, you mean activity, so yes, it works because an Activity is a valid context. If you add this for example the below code in a BroadcastReceiver, the onReceive methods is called with a context that you can use to call getSystemService. If you can, update your post with the code where the above should be run in (activity, broadcastereceiver or somethingelse)
ccheneson
Ok. I put my full code in my original post. As you can see, I tried executing the code in 2 ways, both of which do not work.One using mContext and the other using just service and omitting Context.Thanks a bunch for the help.
Brian Fleishman
Quick update. I got the code to execute successfully by moving it down to the onRecieve section and changing it slightly based on your suggestions:AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); currentRingerMode = manager.getRingerMode(); Log.d(TAG, "ringerMode value=" + currentRingerMode);Thank you very much!!!!!
Brian Fleishman