4

我想在响铃期间更改响铃音量。下面的代码在响铃期间设置它(它在系统设置中更改)但它不会在实际响铃时生效(只有下一个响铃呼叫将使用新值)。我可以强制进行此更改以使其立即生效吗?

AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);    
audio.setStreamVolume(AudioManager.STREAM_RING, volume, AudioManager.FLAG_ALLOW_RINGER_MODES | AudioManager.FLAG_PLAY_SOUND);
4

1 回答 1

4

经过一些研究和测试,我发现只有在来电检测的早期阶段才有可能改变音量。不要在onReceive()方法内部进行太多操作,这一点至关重要。

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");


        }

}

}

以及服务中的一些代码:

// listeners callbacks
public void onPhoneStateChange(boolean state) {

    if (state) {

        if (!callInProgress)    {

       // Do the main task here
        callInProgress = true;
        serviceDestroy();

        }

    } else {

        callInProgress = false;
        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;
    }


}

服务销毁()

// 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;

        }
}
于 2013-01-23T21:14:26.560 回答