2013-01-20 80 views
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

經過一番研究和測試後,我發現只有在非常早的呼入檢測時才能進行音量改變。不要在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; 

     } 
} 
+0

有同樣的問題。那麼,什麼是'來電檢測的早期階段'呢?你可以發佈一些代碼plz? – kiruwka

+0

我有同樣的問題。你能幫我解決嗎?? – Riser

+0

@Riser我已經放了一些代碼。它在一段時間之前工作。 – Kristopher

相關問題