안녕하세요, 안드로이드 초보 개발자입니다.
receiver를 하나 등록해주고 PHONE_STATE action을 수신하게 해놓았습니다.
TelephonyManager 변수를 static으로 receiver에 선언해 놓았습니다.
PHONE_STATE를 수신하면 TelephonyManager 변수의 null 상태를 체크하여 null 인경우 listen을 하게 해놓았습니다.
(LISTEN_CALL_STATE)
근데 일부 단말에서 call state가 오지 않네요 ㅠㅠ
이게... 단말문제인지 코드를 잘못짠게 문제인지 확실하지가 않습니다.
코드에 문제가 없는지 확인 부탁드립니다!!
1. Manifest.xml에 권한 및 receiver 등록.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name=".CallReceiver" android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
2. Receiver 코드.
private static TelephonyManager tm = null;
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
if(tm == null){
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(psm, PhoneStateListener.LISTEN_CALL_STATE);
}else{
Log.i(TAG, "tm is not null");
}
}
}
private PhoneStateListener psm = new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
Log.i(TAG, "onCallStateChanged, state:"+state+", number:"+incomingNumber);
super.onCallStateChanged(state, incomingNumber);
}
};