Exemplo n.º 1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.SMSTransferContactLayout);
            Title = "添加联系电话/邮箱";
            var helper   = new ExtSQLiteHelper(this);
            var editText = FindViewById <EditText>(Resource.Id.contact_text);

            editText.Text = string.Join("\r\n", helper.GetContact());

            #region
            FindViewById <Button>(Resource.Id.save_contact_button).Click += (sender, e) =>
            {
                if (editText != null)
                {
                    var lines = editText.Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    helper.SaveContact(lines);
                }
                Toast.MakeText(this, "完成", ToastLength.Short).Show();
            };
            FindViewById <Button>(Resource.Id.cancel_contact_button).Click += (sender, e) =>
            {
                this.Finish();
            };
            #endregion
        }
Exemplo n.º 2
0
        private void SmsReceiver_MessageCallbackEvent(String number, String body)
        {
            if (helper == null)
            {
                return;
            }
            var config = helper.GetConfig();

            if (config.Count == 0)
            {
                return;
            }
            helper.AddAppLog("[event]:" + (number + "->" + body));

            #region
            try
            {
                #region --代发--
                var controlphone = config["controlphone"] ?? "";//该手机号不转发
                if (!String.IsNullOrEmpty(controlphone) && controlphone.Length > 7 && number.Trim('+').EndsWith(controlphone.Trim('+')))
                {
                    //if (body.StartsWith("#") && body.EndsWith("#"))
                    //{//指令区
                    var cmd   = body.Trim().Trim('#');
                    var param = cmd.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
                    if (param.Length > 1)
                    {
                        SMSHelper.SendPhone(param[0].Trim(), param[1].Trim());
                        helper.UpdateSendStatistics("sendsuccess");
                        helper.AddAppLog("[controlcomplete]:" + body);
                    }
                    else
                    {
                        helper.AddAppLog("[controlerror]接收控制指令:" + cmd);
                    }
                    //}
                    return;
                }
                #endregion

                #region --转发--

                var contactList = helper.GetContact();
                var content     = config["messagetemplate"].Replace("#from#", number).Replace("#body#", body);
                foreach (var contact in contactList)
                {
                    if (contact.Contains("@"))
                    {
                        #region email
                        EmailHelper.Init(config["smtpserver"], config["smtpaccount"], config["smtppassword"], config["smtpaccount"], Convert.ToInt32(config["smtpport"] ?? "0"));
                        var thread = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
                        {
                            try
                            {
                                EmailHelper.Send(string.Format("sms {0}", number), contact, content, null, false);

                                helper.UpdateSendStatistics("sendsuccess");
                                helper.AddAppLog("[emailcomplete]:" + (contact + "->" + content));
                            }
                            catch (Exception ex)
                            {
                                helper.UpdateSendStatistics("sendfailure");
                                helper.AddAppLog("[emailerror]:" + (contact + "->" + content + "->" + ex.Message));
                            }
                        }));
                        thread.Name         = "sendmessagethread";
                        thread.IsBackground = true;
                        thread.Start();
                        #endregion
                    }
                    else
                    {
                        SMSHelper.SendPhone(contact, content);
                        helper.UpdateSendStatistics("sendsuccess");
                        helper.AddAppLog("[smscomplete]:" + (contact + "->" + content));
                    }
                    System.Threading.Thread.Sleep(100);
                }
                #endregion
            }
            catch (Exception ex)
            {
                helper.UpdateSendStatistics("sendfailure");
                helper.AddAppLog("[error]发送失败:" + ex.Message + "(" + body + ")");
            }
            #endregion
        }