views:

60

answers:

1

I have an application that creates a notification with sound when a specific text comes in. To do this, I detect the current ringerMode, change the ringerMode to normal, play the sound, and then quickly return the ringer back to its original setting. Here is the code:

public static AudioManager audio;

    audio = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

ringMode = audio.getRingerMode();
        if (ringMode < 2)
        //Checks to see if the phones ringer is set to silent, or vibrate
            audio.setRingerMode(2);

        //vibrates phone in a pattern
        long[] pattern = {0, 25};
        //long[] pattern = {0, 1000, 100, 100, 100, 100, 100, 1000}; 
        v.vibrate(pattern, -1); 
        final int HELLO_ID = 1;
        mNotificationManager.notify(HELLO_ID, notification);

        //delays return back to original volume so notification can be heard
        Handler handler = new Handler();  
        handler.postDelayed(new Runnable() {  
             public void run() {  
                 audio.setRingerMode(ringMode); 
             }  
        }, 4000);
        }

This works completely fine in an activity, but not in this notification service, and is currently placed in the onStart method. uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" has also been added to the manifest. Any ideas? Thanks

A: 

Not really the solution to that problem, but I realized that there was a Notification volume stream I should have been adjusting. With pretty much exact same code, the Notify class now works properly.

if (startSound)
        {   
            //This sound plays using the ringer volume
            //notification.sound = Uri.parse("android.resource://itp.uts.program/" + R.raw.pager);

            //Sets ringer temporarily to active, so notification is heard
            audio.setStreamVolume(5, 3, 0); 

            //vibrates phone in a pattern
            long[] pattern = {0, 25};
            //long[] pattern = {0, 1000, 100, 100, 100, 100, 100, 1000}; 
            v.vibrate(pattern, -1); 
        }
        //Creates the notification
        mNotificationManager.notify(HELLO_ID, notification);

        if (startSound)
        {
        //delays return back to original volume so notification can be heard
        Handler handler = new Handler();  
        handler.postDelayed(new Runnable() {  
             public void run() {  
                 audio.setStreamVolume(5, streamVolume, 0); 
             }  
        }, 7000);
        }
mbauer14