2017-11-08 18 views
0

통보가 울리면 알림 소리를 내고 싶지 않습니다. 알림 소리가 나고 전화가 울리면 알림 소리를 멈추어야합니다.전화 통보 상태로 통보

다음 코드를 시도했지만 현재 전화 상태를 가져올 수 없습니다.

TelephonyManager telephonyManager = (TelephonyManager) 
context.getSystemService(Context.TELEPHONY_SERVICE); 
telephonyManager.listen(new CustomPhoneStateListener(context), 
PhoneStateListener.LISTEN_CALL_STATE); 

public class CustomPhoneStateListener extends PhoneStateListener { 
    //private static final String TAG = "PhoneStateChanged"; 
    Context context; //Context to make Toast if required 
    public CustomPhoneStateListener(Context context) { 
     super(); 
     this.context = context; 
    } 

    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 
     super.onCallStateChanged(state, incomingNumber); 

     switch (state) { 
     case TelephonyManager.CALL_STATE_IDLE: 
      //when Idle i.e no call 
      Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show(); 
      break; 
     case TelephonyManager.CALL_STATE_OFFHOOK: 
      //when Off hook i.e in call 
      //Make intent and start your service here 
      Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show(); 
      break; 
     case TelephonyManager.CALL_STATE_RINGING: 
      //when Ringing 
      Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show(); 
      break; 
     default: 
      break; 
     } 
    } 
} 

답변

1

이 같은이 목적을 위해 브로드 캐스트 리시버 사용하고 상태에 따라 알림 해제 할 수 있습니다 : 전화 통화 중에

<receiver android:name=".PhonecallReceiver" > 
    <intent-filter> 
     <action android:name="android.intent.action.PHONE_STATE" /> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
    </intent-filter> 
</receiver> 
+0

안녕하세요 @masoudVali 내가 다른 사용자에게 전화 할 때 어떤 상태가됩니까? –

+0

도움을 주셔서 감사합니다. 코드가 잘 작동합니다. –

+0

이 'intent.getAction(). equals (Intent.ACTION_NEW_OUTGOING_CALL)'에 의해 발신 전화를 확인할 수 있습니다. –

0

:

public class PhonecallReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
     String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE); 
     int state = 0; 
     if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){ 
      state = TelephonyManager.CALL_STATE_IDLE; 
     } 
     else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){ 
      state = TelephonyManager.CALL_STATE_OFFHOOK; 
     } 
     else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){ 
      state = TelephonyManager.CALL_STATE_RINGING; 
     } 

    } 
} 

과 manifest.xml 파일을 , onCallStateChanged() 전화 상태를 확인할 수 있습니다. 알림을 중지하려면 다음을 시도하십시오.

try { 
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), notification); 
ringtone .stop(); 
} catch (Exception e) { 
e.printStackTrace(); 
} 

해피 코딩!