Phone call state broadcast receiver

/* Add read phone state to your AndroidManifest.xml file */ <uses-permission android:name="android.permission.READ_PHONE_STATE"/> /* Register the BroadcastReceiver */ <receiver android:name="net.mobileway.service.PhoneStateBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"/> </intent-filter> </receiver> /* The broadcast receiver class */ public class PhoneStateBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "PhoneStateBroadcastReceiver"; @Override public void onReceive(Context context, Intent intent) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE); } class CustomPhoneStateListener extends PhoneStateListener { private Context context; 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 Log.e(TAG, "Phone state: idle - online"); break; case TelephonyManager.CALL_STATE_OFFHOOK: // when Off hook i.e in call Log.e(TAG, "Phone state: off hook - in call"); break; case TelephonyManager.CALL_STATE_RINGING: // when Ringing Log.e(TAG, "Phone state: ringing"); break; default: break; } } } }
Know when the Android device receives a phone call (gsm), when the phone is ringing, or when the call is ended.

#android #snippet #phoneCall #call

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.