Android setStreamVolume for STREAM_RING doesn't always work while RINGING

末鹿安然 提交于 2019-12-07 22:49:47

问题


I am trying to do this simple App that allows the phone to ring but not to play a sound when receiving notifications under Android 4 (ICS). In order to do that I set the ring/notification stream to 0 (mute). When receiving a call I set the ring volume to max (p.e. 7) and then, on Idle, I turn it back to 0 (mute).

To do that I use this simple code.

public class ReceiverClass extends BroadcastReceiver
{   
   @Override public void onReceive(Context context, Intent intent)
   {      
      if ("android.intent.action.PHONE_STATE".equals(intent.getAction()))
      {  String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);      
         AudioManager amanager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

         if (TelephonyManager.EXTRA_STATE_IDLE.equals(state))
         {  amanager.setStreamVolume(AudioManager.STREAM_RING, 0, 0);           
         }
         else if (TelephonyManager.EXTRA_STATE_RINGING.equals(state))
         {  amanager.setStreamVolume(AudioManager.STREAM_RING, 7, 0);           
         }        
      }
   }  
}

The problem is that it sometime works and it sometime doesn't. I cannot find a reason. I call myself ten times and the ring sounds maybe 6. The other 4 were muted. strange??

I understand that my application could take "too long" to call setStreamVolume to 7 so maybe that is way sometimes gets on time (and sounds) and sometimes it is too late (so it doesn't sound) because the call already started with 0 volume. So, is there a way to restart the ringing sound? Could anybody help me with this? Thanks.


回答1:


I must say you should try giving your Broadcast Receiver high priority. which can be done like below

  <intent-filter android:priority="1000">
   <action android:name="android.intent.action.PHONE_STATE" />
 </intent-filter>     

refer the link >> Highest priority

It can/may solve your problem.



来源:https://stackoverflow.com/questions/11274105/android-setstreamvolume-for-stream-ring-doesnt-always-work-while-ringing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!