经过一些研究和测试,我发现只有在来电检测的早期阶段才有可能改变音量。不要在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;
}
}