Changing ringer volume during ringing

后端 未结 1 1092
心在旅途
心在旅途 2021-01-13 22:17

I\'d like to change the ringer volume during ringing. Code below sets it during ringing (it is changed in system settings) but it doesn\'t come to effect with actual ringing

相关标签:
1条回答
  • 2021-01-13 23:20

    After some research and tests I've found that volume change is possible only in very early stage of incomming call detection. It's vital not to do much operations inside the onReceive() method.

    public class CallReceiver extends BroadcastReceiver   {
    
    private boolean isRinging = false;
    private boolean isInProgress = false;
    private Listener mListener = null;
    
    // interface declaration
     public interface Listener {
            public void onPhoneStateChange(boolean state);
        }
    
    // registration of listener
     public void registerListener (Listener listener) {
            mListener = listener;
        }
    
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
    
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    
            if(TelephonyManager.EXTRA_STATE_RINGING.equals(state))  {
                // do something while ringing BUT NOT TOO MUCH!
    
                isRinging = true;
    
                if (context instanceof StateChecker)
                      ((StateChecker) context).serviceDestroy();
                    Log.v("phone", "ringing");
    
            }
    
    
            else if(TelephonyManager.EXTRA_STATE_IDLE.equals(state))    {
                //do something when the call ends
                if (context instanceof StateChecker)
                      ((StateChecker) context).serviceStart();
                Log.v("phone", "idle");
                isRinging = false;
            }
    
    
            // sending notification to listener
            if(mListener != null)   {
                mListener.onPhoneStateChange(isRinging);
    
                Log.v("listener", "notified");
    
    
            }
    
    }
    
    }
    

    and some code from service:

    // listeners callbacks
    public void onPhoneStateChange(boolean state) {
    
        if (state) {
    
            if (!callInProgress)    {
    
           // Do the main task here
            callInProgress = true;
            serviceDestroy();
    
            }
    
        } else {
    
            callInProgress = false;
            serviceStart();
    
        }
    }
    

    ServiceStart:

     // starting service
    public void serviceStart()  {
    
        preferencesRead();
    
        //The intent to launch when the user clicks the expanded notification
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
    
    
    
        if(settings.getBoolean("hide_noti_icon", false))    {
         Notification noti = new NotificationCompat.Builder(this)
         .setContentTitle("Intelligent Ringer")
         .setContentText(getString(R.string.is_running))
         .setSmallIcon(R.drawable.invisible)
         .setContentIntent(pendIntent)
         .setPriority(Notification.PRIORITY_MIN)
         .build();
    
         startForeground(12345, noti); 
    
    
        } else  {
    
             Notification noti = new NotificationCompat.Builder(this)
             .setContentTitle("Intelligent Ringer")
             .setContentText(getString(R.string.is_running))
             .setSmallIcon(R.drawable.ic_launcher)
             .setContentIntent(pendIntent)
             .setPriority(Notification.PRIORITY_MIN)
             .build();
    
             startForeground(12345, noti); 
        }
    
    
        Log.v("service", "started");
        // registering listener for CallReceiver
        if(isCRRegistered)  {
            unregisterReceiver(mCallReceiver);
            isCRRegistered = false;
        }
    
        if(!isCRRegistered) {
    
         IntentFilter filter2 = new IntentFilter("android.intent.action.PHONE_STATE");
         filter2.addAction("android.intent.action.PHONE_STATE");
         CallReceiver mCallReceiver = new CallReceiver();
         this.mCallReceiver = mCallReceiver;
           registerReceiver(mCallReceiver, filter2);
           mCallReceiver.registerListener(this);
           isCRRegistered = true;
        }
    
        if(isScreenRegistered)  {
            unregisterReceiver(mReceiver);
            isScreenRegistered = false;
        }
    
    
    
        if(!isPositionRegistered && proximityOn)    {
    
            Position mPosition = new Position(this.getApplicationContext());
            mPosition.registerListener(this);
            mPosition.isInPocket();
            this.mPosition = mPosition;
            mPosition.register();
    
            isPositionRegistered = true;
    }
    
        if(!isScreenRegistered) {
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            ScreenReceiver mReceiver = new ScreenReceiver();
            registerReceiver(mReceiver, filter);
            this.mReceiver = mReceiver;
    
            mReceiver.registerListener(this);
    
            isScreenRegistered = true;
        }
    
    
    }
    

    Servicedestroy()

    // stopping service
    public void serviceDestroy()    {
    
    
        if (this.mPosition != null) {
        this.mPosition.unregister();
        }
    
        Log.v("service", "Destroyed");
    
        if(isScreenRegistered)  {
            unregisterReceiver(mReceiver);
            isScreenRegistered = false;
        }
    
    }
    

    OnDestroy()

    public void onDestroy () {
        serviceDestroy();
    
    
        if(isCRRegistered)  {
    
            unregisterReceiver(mCallReceiver);
            isCRRegistered = false;
    
            }
    }
    
    0 讨论(0)
提交回复
热议问题