Skip to content

Instantly share code, notes, and snippets.

@allieus
Created March 21, 2014 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allieus/9691417 to your computer and use it in GitHub Desktop.
Save allieus/9691417 to your computer and use it in GitHub Desktop.
전화 CallReceiver
package com.example
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.telephony.PhoneStateListener
import android.telephony.ServiceState
import android.telephony.TelephonyManager
import kotlin.properties.Delegates
import kotlinLib.*
class CallReceiver: BroadcastReceiver() {
class object {
val TAG = javaClass<CallReceiver>().getSimpleName()
}
var context: Context by Delegates.notNull()
override fun onReceive(context: Context, intent: Intent) {
log(TAG, "onReceive() - ${intent.getAction()}")
this.context = context
val telephony = getTelephonyService(context)!!
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE)
// telephony.listen(listener, PhoneStateListener.LISTEN_SERVICE_STATE)
// telephony.listen(listener, PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR)
if ( intent.getAction() == Intent.ACTION_NEW_OUTGOING_CALL ) {
val phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)
notify("RECEIVE_OUTGOING_NUMBER", phoneNumber)
}
}
fun notify(callEventType: String, callNumber: String? = null) {
log(TAG, "eventType : ${callEventType}, number : ${callNumber}")
val intent = Intent(context, javaClass<CallService>())
intent.putExtra("callEventType", callEventType)
intent.putExtra("callNumber", callNumber)
context.startService(intent) // Service 의 onStartCommand() 메서드를 호출
}
val listener = object: PhoneStateListener() {
// 발신 : IDLE -> OFFHOOK (신호가 갈때 이미 offhook) -> IDLE
// 수신 : IDLE -> RINGING (번호가 뜸) -> OFFHOOK -> IDLE
// 전화통화 상태가 변경될 때 호출
override fun onCallStateChanged(state: Int, incomingNumber: String?) {
when ( state ) {
TelephonyManager.CALL_STATE_IDLE -> { notify("IDLE") } // 폰이 울리거나 통화 중이 아님.
TelephonyManager.CALL_STATE_RINGING -> { notify("RINGING", incomingNumber) } // 폰이 울린다.
TelephonyManager.CALL_STATE_OFFHOOK -> { notify("OFFHOOK") } // 현재 통화 중
}
}
// override fun onCallForwardingIndicatorChanged(cfi: Boolean) {} // 통화 전달
// override fun onCellLocationChanged(location: CellLocation) {} // 셀위치변경
// override fun onDataActivity(direction: Int) {}
// override fun onDataConnectionStateChanged(state: Int) {} // 데이터 연결
// override fun onSignalStrengthChanged(asu: Int) {} // 모바일신호세기
// override fun onMessageWaitingIndicatorChanged(mwi: Boolean) {} // 대기
// override fun onServiceStateChanged(serviceState: ServiceState?) {} // 전화 서비스 상태가 변경될 때 호출
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment