• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps I have a question.

Jsin

Lurker
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..

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.
 
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..

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.

Mind if I ask what book?
Him making you code out something that already exists in every phone makes me think you might be getting scammed by a "self taught genius" that decided to make money off the minimalistic amount they learned.
 
Well guys I found out what the problem is, there is a bug in 2.2 that when you use the audioManager.setStreamMute it never unmutes. So what I did was just grab the current volume and then when I undid the mute I just set the volume back to what it was.
 
Back
Top Bottom