mercredi 23 décembre 2015

Save a Number detecting if a phone call was answered or not Xamarin.Android

My goal is to save an incoming phone number in a Sqlite database detecting if the call was answered or missed / rejected.

I subscribed to a BradcastReceiver, but it doesn't work as expected...it will save the number in both databases.

This is my receiver:

        public override void OnReceive(Context context, Intent intent)
                {

                    // ensure there is information
                    if (intent.Extras != null)
                    {

                        // get the incoming call state
                        string state = intent.GetStringExtra(TelephonyManager.ExtraState);

                        // check the current state
                        if (state == TelephonyManager.ExtraStateRinging)
                        {
                            // read the incoming call telephone number...
                            telephone = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);
                             //Remove country code
                            telephoneShort = telephone.Substring(3);
                            // check the reade telephone
                            if (string.IsNullOrEmpty(telephone))
                                telephone = string.Empty;



                        }

                        // incoming call is answered
                        if (state == TelephonyManager.ExtraStateOffhook)
                        {
//check if the number is in contacts database and save it with its name
                            Risposta = sqldb.QueryIt(telephoneShort);
                            if (Risposta != null)
                            {
                                sqldbRicevute.AddRecord(Risposta, DateTime.Now.Month.ToString());


                            }
                            else
                                sqldbRicevute.AddRecord(telephoneShort, DateTime.Now.Month.ToString());



                        }

                        //Save a missed call
                        else
                        {
                            Risposta = sqldb.QueryIt(telephoneShort);
                            sqldbPerse.AddRecord(Risposta, telephoneShort);
                        }

                        // incoming call ended
                        if (state == TelephonyManager.ExtraStateIdle)
                        {


                            //Do cool stuff, like unicorns and rainbows when call ends...I think
                            key.Load();


                        }
                    }
                }

Do you guys see what's wrong with my code?? Thank you so much in advance!!!

Aucun commentaire:

Enregistrer un commentaire