일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- push
- 페이백
- swift3
- Xcode
- 포켓몬 GO
- 앱스토어
- UIView
- 신도림 테크노마트
- afterdelay
- 얻는법
- UITableView
- loop
- 포켓볼
- 해몽
- Bitcode
- simulator
- error
- Example
- GCD
- 아이폰7
- swift
- 신도림
- 스마트폰
- 샘플
- LG유플러스
- 보라카이
- 공략
- IOS
- Check
- setting
- Today
- Total
도래울
Get Phone State When Someone is calling using BroadcastReceiver Example 본문
Get Phone State When Someone is calling using BroadcastReceiver Example
도래울 2016. 2. 5. 13:14 this article we shall try to listen to the phone state when contacts are calling us.
First of all we need to set our Manifest file to listen to the Phone State, to do that we need to edit our it.
<application> ..... <receiver android:name=".ServiceReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver> </application> <uses-permission android:name="android.permission.READ_PHONE_STATE"> </uses-permission>
Here you can see that we created a receiver xml node inside our application and have the java class ServiceReceiver to listen to it. What it would listen to is the PHONE_STATE and thus we need the permission of READ_PHONE_STATE. Then our ServiceReceiver Class would look like this.
public class ServiceReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { MyPhoneStateListener phoneListener=new MyPhoneStateListener(); TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE); } }
For the full class including the imports, please download the files below. In here we have another class called MyPhoneStateListener, which would be shown at the bottom. What this class would do is execute the phoneListener when the telephony.listen has received a LISTEN_CALL_STATE.
public class MyPhoneStateListener extends PhoneStateListener { public void onCallStateChanged(int state,String incomingNumber){ switch(state){ case TelephonyManager.CALL_STATE_IDLE: Log.d("DEBUG", "IDLE"); break; case TelephonyManager.CALL_STATE_OFFHOOK: Log.d("DEBUG", "OFFHOOK"); break; case TelephonyManager.CALL_STATE_RINGING: Log.d("DEBUG", "RINGING"); break; } } }
What we have is a function called onCallStateChanged which would be fired when the LISTEN_CALL_STATE dispatches it. The states are either, ringing(CALL_STATE_RINGING), answers (CALL_STATE_OFFHOOK), or hang up/end call (CALL_STATE_IDLE). To see the logs in eclipse. Go to Window -> Show View -> Other -> Android -> LogCat
'개발 > Android' 카테고리의 다른 글
안드로이드 Notification 이 Clear 되지 않도록 하는 방법 (0) | 2016.02.05 |
---|---|
[Android] Notification (0) | 2016.02.05 |
Android Eclipse로 Debugging (0) | 2016.02.05 |
Android Service Lifecycle (0) | 2016.02.05 |
안드로이드 Activity LifeCycle (0) | 2016.02.05 |