Ok guys so I am working on through this book and it has me making a silent mode app. So I took this example and have been modifying it to do what I want. The app itself works like it is intended to, but it's the widget that I am having trouble with. The problem I am having is when I toggle the silent off I have it where the ringer is set to vibrate and the multimedia is set to mute. So when I toggle the widget back so the sound is on, my multimedia sound never comes back on and when I go into settings I am not able to manually turn the sound up. Here is code for the widget..
Thanks in advance for any guidance you can give.
Code:
public class AppWidget extends AppWidgetProvider{
@Override
public void onReceive(Context context, Intent intent){
if (intent.getAction() == null){
context.startService(new Intent (context, ToggleService.class));
}else {
super.onReceive(context, intent);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
context.startService(new Intent(context, ToggleService.class));
}
public static class ToggleService extends IntentService {
public ToggleService(){
super("AppWidget$ToggleService");
}
@Override
protected void onHandleIntent(Intent intent){
ComponentName me = new ComponentName(this, AppWidget.class);
AppWidgetManager mgr = AppWidgetManager.getInstance(this);
mgr.updateAppWidget(me, buildUpdate(this));
}
private RemoteViews buildUpdate(Context cntxt){
RemoteViews updateViews = new RemoteViews (cntxt.getPackageName(), R.layout.widget);
AudioManager audioManager = (AudioManager)cntxt.getSystemService(Activity.AUDIO_SERVICE);
if(audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE
|| audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT ){
updateViews.setImageViewResource(R.id.phoneState, R.drawable.phone_state_normal);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}else{
updateViews.setImageViewResource(R.id.phoneState, R.drawable.phone_state_silent);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
Intent i = new Intent(this, AppWidget.class);
PendingIntent pi = PendingIntent.getBroadcast(cntxt, 0, i, 0);
updateViews.setOnClickPendingIntent(R.id.phoneState, pi);
return updateViews;
}
}
Thanks in advance for any guidance you can give.