TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if(mgr != null) { mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); }
private void stopTelephonyListener() { if (mTelephonyManager != null && mPhoneStateListener != null) { mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE); mTelephonyManager = null; mPhoneStateListener = null; } } }
@Override public void listen(final PhoneStateListener listener, final int events) { if ((events & UNSUPPORTED_LISTEN_EVENTS) != 0) { if (BuildConfig.DEBUG) throw new UnsupportedOperationException("One of the event type is not supported due to permission READ_PHONE_STATE required: " + events); super.listen(listener, events & ~ UNSUPPORTED_LISTEN_EVENTS); } else super.listen(listener, events); }
public class ServiceReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); telephony.listen(new PhoneStateListener(){ @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); System.out.println("incomingNumber : "+incomingNumber); } },PhoneStateListener.LISTEN_CALL_STATE); } }
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; import android.widget.Toast; public class PhoneCallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(context); telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); }}
EndCallListener callListener = new EndCallListener(); TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
PhoneStateListener phoneStateListener = new PhoneStateListener() { @Override public void onCallStateChanged(int state, String incomingNumber) { if (state == TelephonyManager.CALL_STATE_RINGING) { //Incoming call: Pause music } else if(state == TelephonyManager.CALL_STATE_IDLE) { //Not in call: Play music } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) { //A call is dialing, active or on hold } super.onCallStateChanged(state, incomingNumber); } }; TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if(mgr != null) { mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); }
StateListener phoneStateListener = new StateListener(); TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); } catch (Exception e) { e.printStackTrace();
@Test public void testListenInit() { PhoneStateListener listener = mock(PhoneStateListener.class); telephonyManager.listen(listener, LISTEN_CALL_STATE | LISTEN_CELL_INFO | LISTEN_CELL_LOCATION); verify(listener).onCallStateChanged(CALL_STATE_IDLE, null); verify(listener).onCellLocationChanged(null); if (VERSION.SDK_INT >= JELLY_BEAN_MR1) { verify(listener).onCellInfoChanged(Collections.emptyList()); } }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void shouldGiveAllCellInfo() { PhoneStateListener listener = mock(PhoneStateListener.class); telephonyManager.listen(listener, LISTEN_CELL_INFO); List<CellInfo> allCellInfo = Collections.singletonList(mock(CellInfo.class)); shadowOf(telephonyManager).setAllCellInfo(allCellInfo); assertEquals(allCellInfo, telephonyManager.getAllCellInfo()); verify(listener).onCellInfoChanged(allCellInfo); }
@Test @Config(minSdk = P) public void shouldGiveSignalStrength() { PhoneStateListener listener = mock(PhoneStateListener.class); telephonyManager.listen(listener, LISTEN_SIGNAL_STRENGTHS); SignalStrength ss = Shadow.newInstanceOf(SignalStrength.class); shadowOf(telephonyManager).setSignalStrength(ss); verify(listener).onSignalStrengthsChanged(ss); } }
@Test public void shouldGiveCellLocation() { PhoneStateListener listener = mock(PhoneStateListener.class); telephonyManager.listen(listener, LISTEN_CELL_LOCATION); CellLocation mockCellLocation = mock(CellLocation.class); shadowOf(telephonyManager).setCellLocation(mockCellLocation); assertEquals(mockCellLocation, telephonyManager.getCellLocation()); verify(listener).onCellLocationChanged(mockCellLocation); }
@Test public void shouldGiveCallState() { PhoneStateListener listener = mock(PhoneStateListener.class); telephonyManager.listen(listener, LISTEN_CALL_STATE); shadowOf(telephonyManager).setCallState(CALL_STATE_RINGING, "911"); assertEquals(CALL_STATE_RINGING, telephonyManager.getCallState()); verify(listener).onCallStateChanged(CALL_STATE_RINGING, "911"); shadowOf(telephonyManager).setCallState(CALL_STATE_OFFHOOK, "911"); assertEquals(CALL_STATE_OFFHOOK, telephonyManager.getCallState()); verify(listener).onCallStateChanged(CALL_STATE_OFFHOOK, null); }
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR | PhoneStateListener.LISTEN_CALL_STATE | PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_DATA_ACTIVITY | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE | PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR | PhoneStateListener.LISTEN_SERVICE_STATE | PhoneStateListener.LISTEN_SIGNAL_STRENGTH);
// Be careful by calling getActualDefaultRingtoneUri in CallListener, it could return null, better way to save it in OnCreate mOldUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE); TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); mTelephonyMgr.listen(new MyCallListener(), PhoneStateListener.LISTEN_CALL_STATE);
@Override public void listen(final PhoneStateListener listener, final int events) { if ((events & UNSUPPORTED_LISTEN_EVENTS) != 0) { if (BuildConfig.DEBUG) throw new UnsupportedOperationException("One of the event type is not supported due to permission READ_PHONE_STATE required: " + events); super.listen(listener, events & ~ UNSUPPORTED_LISTEN_EVENTS); } else super.listen(listener, events); }
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); AndroidPhoneStateListener phoneStateListener = new AndroidPhoneStateListener(text); telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
public class CallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { MyPhoneStateListener phoneListener = new MyPhoneStateListener(context); TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); } }
PhoneCallListener phoneListener = new PhoneCallListener(); TelephonyManager telephonyManager = (TelephonyManager) this .getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);