How to listen for Incoming and Outgoing call and perform our task accordingly. Here is how to do it:
public class MyPhoneStateListener extends Service{
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
StateListener phoneStateListener = new StateListener();
TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}catch(Exception e){ }
}
class StateListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
};
@Override
public void onDestroy() {
}
}
Note: While stopping service don't foget to remove the listener and add these permissions to your manifest file:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
No comments:
Post a Comment