我想在响铃期间改变铃声音量。下面的代码在振铃期间设置它(在系统设置中更改)但它不会在实际振铃时生效(只有下一个振铃呼叫将使用新值)。我可以强制进行此更改以使其立即生效吗?
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audio.setStreamVolume(AudioManager.STREAM_RING, volume, AudioManager.FLAG_ALLOW_RINGER_MODES | AudioManager.FLAG_PLAY_SOUND);
答案 0 :(得分: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();
}
}
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;
}
}