在android中收到如何检测外出电话

时间:2015-11-24 09:07:31

标签: java android telephonymanager phone-call

我尝试了很多解决方案,我需要检测何时接听或拒绝接听电话,任何建议都会对我有所帮助。我试图使用布尔值检测。请帮我解决一下。

我的代码:

public class CallerToActivity extends BroadcastReceiver {
    // static boolean wasRinging;
    static boolean finish = false;
    static Context mcontext;

    public void onReceive(Context context, Intent intent) {

        PhoneStateChangeListener pscl = new PhoneStateChangeListener();
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(pscl, PhoneStateListener.LISTEN_CALL_STATE);
        mcontext=context;
    }

    private static class PhoneStateChangeListener extends PhoneStateListener {
        static boolean wasRinging;

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                Log.i("Status", "RINGING");
                wasRinging = true;
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.i("Status", "OFFHOOK");

                if (wasRinging == false) {
                    // Start your new activity

                } else {
                    // Cancel your old activity
                }

                // this should be the last piece of code before the break
                wasRinging = true;
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                Log.i("Status", "IDLE");
                // this should be the last piece of code before the break
                wasRinging = false;
                break;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

以下是我用来做这件事的事情:

清单:

      org.springframework.core.convert.ConversionFailedException: Failed to convert from type @javax.persistence.Column @org.springframework.format.annotation.DateTimeFormat java.sql.Date to type java.lang.String for value '1978-07-01'; nested exception is java.lang.IllegalStateException: JodaTime library not available - @DateTimeFormat not supported
        org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
        org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:171)
        org.springframework.validation.AbstractPropertyBindingResult.formatFieldValue(AbstractPropertyBindingResult.java:125)
        org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:222)
        org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:151)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:142)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:126)
        org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)
        org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
        org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
        org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
        org.apache.jsp.WEB_002dINF.views.persona_jsp._jspx_meth_form_005finput_005f5(persona_jsp.java:317)
        org.apache.jsp.WEB_002dINF.views.persona_jsp._jspx_meth_form_005fform_005f0(persona_jsp.java:135)
        org.apache.jsp.WEB_002dINF.views.persona_jsp._jspService(persona_jsp.java:76)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
        org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:688)
        org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:682)
        org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:103)
        org.apache.tiles.jsp.context.JspTilesRequestContext.dispatch(JspTilesRequestContext.java:96)
        org.apache.tiles.renderer.impl.UntypedAttributeRenderer.write(UntypedAttributeRenderer.java:61)
        org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:669)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:337)
        org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:234)
        org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:211)
        org.apache.tiles.jsp.taglib.RenderTag.doEndTag(RenderTag.java:220)
        org.apache.jsp.WEB_002dINF.template.default_.template1_jsp._jspx_meth_tiles_005finsertAttribute_005f2(template1_jsp.java:284)
        org.apache.jsp.WEB_002dINF.template.default_.template1_jsp._jspService(template1_jsp.java:115)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:241)
        org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222)
        org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
        org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:669)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:689)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:643)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:626)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:322)
        org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124)
        org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
        org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
        org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
        org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
        org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
        org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
        org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
        org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
        org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
        org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
        org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)



    root cause 
    java.lang.IllegalStateException: JodaTime library not available - @DateTimeFormat not supported
        org.springframework.format.support.DefaultFormattingConversionService$NoJodaDateTimeFormatAnnotationFormatterFactory.getPrinter(DefaultFormattingConversionService.java:127)
        org.springframework.format.support.DefaultFormattingConversionService$NoJodaDateTimeFormatAnnotationFormatterFactory.getPrinter(DefaultFormattingConversionService.java:1)
        org.springframework.format.support.FormattingConversionService$AnnotationPrinterConverter.convert(FormattingConversionService.java:221)
        org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35)
        org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:171)
        org.springframework.validation.AbstractPropertyBindingResult.formatFieldValue(AbstractPropertyBindingResult.java:125)
        org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:222)
        org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:151)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:142)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:126)
        org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)
        org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
        org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
        org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
        org.apache.jsp.WEB_002dINF.views.persona_jsp._jspx_meth_form_005finput_005f5(persona_jsp.java:317)
        org.apache.jsp.WEB_002dINF.views.persona_jsp._jspx_meth_form_005fform_005f0(persona_jsp.java:135)
        org.apache.jsp.WEB_002dINF.views.persona_jsp._jspService(persona_jsp.java:76)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
        org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:688)
        org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:682)
        org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:103)
        org.apache.tiles.jsp.context.JspTilesRequestContext.dispatch(JspTilesRequestContext.java:96)
        org.apache.tiles.renderer.impl.UntypedAttributeRenderer.write(UntypedAttributeRenderer.java:61)
        org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:669)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:337)
        org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:234)
        org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:211)
        org.apache.tiles.jsp.taglib.RenderTag.doEndTag(RenderTag.java:220)
        org.apache.jsp.WEB_002dINF.template.default_.template1_jsp._jspx_meth_tiles_005finsertAttribute_005f2(template1_jsp.java:284)
        org.apache.jsp.WEB_002dINF.template.default_.template1_jsp._jspService(template1_jsp.java:115)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:241)
        org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222)
        org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
        org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:669)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:689)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:643)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:626)
        org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:322)
        org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124)
        org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
        org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
        org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
        org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
        org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
        org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
        org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
        org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
        org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)


org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)

我的电话呼叫检测器

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

<!--This part is inside the application-->
    <receiver android:name=".CallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

然后使用它,只需从中派生一个类并实现一些简单的函数,无论你关心哪种调用类型:

package com.xyz.android.reusable.receivers;

import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

public abstract class PhonecallReceiver extends BroadcastReceiver {

    //The receiver will be recreated whenever android feels like it.  We need a static variable to remember data between instantiations

    private static int lastState = TelephonyManager.CALL_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static String savedNumber;  //because the passed incoming is only valid in ringing


    @Override
    public void onReceive(Context context, Intent intent) {

        //We listen to two intents.  The new outgoing call only tells us of an outgoing call.  We use it to get the number.
        if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
            savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
        }
        else{
            String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
            String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            int state = 0;
            if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                state = TelephonyManager.CALL_STATE_IDLE;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                state = TelephonyManager.CALL_STATE_OFFHOOK;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                state = TelephonyManager.CALL_STATE_RINGING;
            }


            onCallStateChanged(context, state, number);
        }
    }

    //Derived classes should override these to respond to specific events of interest
    protected void onIncomingCallStarted(Context ctx, String number, Date start){}
    protected void onOutgoingCallStarted(Context ctx, String number, Date start){}
    protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end){}
    protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end){}
    protected void onMissedCall(Context ctx, String number, Date start){}

    //Deals with actual events

    //Incoming call-  goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
    //Outgoing call-  goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
    public void onCallStateChanged(Context context, int state, String number) {
        if(lastState == state){
            //No change, debounce extras
            return;
        }
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                isIncoming = true;
                callStartTime = new Date();
                savedNumber = number;
                onIncomingCallStarted(context, number, callStartTime);
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                //Transition of ringing->offhook are pickups of incoming calls.  Nothing done on them
                if(lastState != TelephonyManager.CALL_STATE_RINGING){
                    isIncoming = false;
                    callStartTime = new Date();
                    onOutgoingCallStarted(context, savedNumber, callStartTime);                     
                }
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                //Went to idle-  this is the end of a call.  What type depends on previous state(s)
                if(lastState == TelephonyManager.CALL_STATE_RINGING){
                    //Ring but no pickup-  a miss
                    onMissedCall(context, savedNumber, callStartTime);
                }
                else if(isIncoming){
                    onIncomingCallEnded(context, savedNumber, callStartTime, new Date());                       
                }
                else{
                    onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());                                               
                }
                break;
        }
        lastState = state;
    }
}