65.9K
CodeProject 正在变化。 阅读更多。
Home

PocketPcAgent

starIconstarIconstarIconstarIconstarIcon

5.00/5 (12投票s)

2009年9月8日

CPOL

3分钟阅读

viewsIcon

29212

downloadIcon

288

PocketPcAgent 是一个可以安装在运行 Windows Mobile 5.0 或更高版本的 Windows Mobile 设备上的应用程序。该应用程序侦听事件,例如收到的短信、未接来电、收到的电子邮件、电池强度变化等等。

引言

PocketPcAgent 是一个可以安装在运行 Windows Mobile 5.0 或更高版本的 Windows Mobile 设备上的应用程序。运行时,该应用程序侦听事件,例如收到的短信、未接来电、收到的电子邮件、电池强度变化等等。用户可以订阅事件并选择在通过电子邮件、短信或电话呼叫等渠道引发事件时收到通知。

背景

开发此软件的主要目的是帮助携带两部手机的人。有时您可能没有随身携带您的其中一部手机。在这种情况下,您不知道是谁给您打电话或给您发送了消息。我想,如果可以通过您随身携带的移动设备或通过电子邮件收到此类情况的通知,那不是很好吗?我的回答是这将非常棒,所以我开发了这个应用程序。

Using the Code

当您打开解决方案时,您将看到三个主要文件夹。在Agents文件夹中,您可以找到侦听 Pocket PC 上 SMS、电话或通用事件的 Agent 类。helpers文件夹中的类提供诸如发送 SMS、呼叫、序列化和发送电子邮件等功能。并且,objects文件夹中的类主要用于存储用户设置和管理我上面提到的其他类。

让我们先预览一下 helper 类

private static void AddRecipient(SmsMessage parSmsMessage, string parRecipient)
{
    AddRecipient(parSmsMessage, new Recipient(parRecipient));
}
public static void SendSms(string parBody, string parRecipient)
{
    SmsMessage insSmsMessage = new SmsMessage();
    insSmsMessage.Body = parBody;
    AddRecipient(insSmsMessage, parRecipient);
    insSmsMessage.Send();
}

private static string GetAccountName()
{
    OutlookSession insOutlookSession = new OutlookSession();
    return insOutlookSession.EmailAccounts[0].Name;
}
private static void AddRecipient(EmailMessage parEmailMessage, string parRecipient)
{
    AddRecipient(parEmailMessage, new Recipient(parRecipient));
}
public static void SendEmail(string parBody, string parRecipient)
{
    EmailMessage insEmailMessage = new EmailMessage();
    insEmailMessage.BodyText = parBody;
    AddRecipient(insEmailMessage, parRecipient);
    insEmailMessage.Send(GetAccountName());
}

public static void Call(string parNumber, bool parShowPrompt)
{
    Microsoft.WindowsMobile.Telephony.Phone p = 
              new Microsoft.WindowsMobile.Telephony.Phone();
    p.Talk(parNumber, parShowPrompt);
}

如您所见,helper 类提供了可以在引发事件时执行的功能。Helper 类也有这些方法的重载。通过下载源代码,您也可以查看它们。

现在,让我们看看 Agent 类。SmsAgent类使用MessageInterceptor对象来侦听收到的消息,并且当收到类型为SmsMessage的消息时,将引发此类的SmsRecieved事件。

public class SmsAgent
{
    MessageInterceptor msgInterceptor = 
      new MessageInterceptor(InterceptionAction.Notify, true);
    public delegate void SmsReceivedDelegate(SmsMessage msg);
    public event SmsReceivedDelegate SmsReceived;

    public SmsAgent()
    {
        msgInterceptor.MessageReceived += 
          new MessageInterceptorEventHandler(msgInterceptor_MessageReceived);
    }
    void msgInterceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
    {
        if (e.Message is Microsoft.WindowsMobile.PocketOutlook.SmsMessage)
        {
            if (SmsReceived != null)
            {
                SmsReceived((SmsMessage)e.Message);
            }
        }
    }
}

EmailAgent类的工作方式与SmsAgent相同,但仅检查消息类型是否为EmailMessage

TelephonyAgent类使用SystemState对象来侦听PhoneMissedCalls属性。当此属性更改时,将引发此类的CallMissed事件。

public class TelephonyAgent
{
    public delegate void CallMissedDelegate(string callerName, 
                                            string callerNumber);
    public event CallMissedDelegate CallMissed;
    SystemState insSystemState = 
                new SystemState(SystemProperty.PhoneMissedCalls);

    Guid insGuid = Guid.NewGuid();
    
    public TelephonyAgent()
    {
        insSystemState.Changed += new ChangeEventHandler(phoneMissedCalls_Changed);
    }

    void phoneMissedCalls_Changed(object sender, ChangeEventArgs args)
    {
        if (CallMissed != null)
        {
            if (SystemState.PhoneLastIncomingCallerNumber == 
                               Helpers.TelephonyHelper.ActionCall)
            {
                Helpers.TelephonyHelper.ActionCall = "";
            }
            else
            {
                CallMissed(SystemState.PhoneLastIncomingCallerName, 
                           SystemState.PhoneLastIncomingCallerNumber);
            }
        }
    }
}

GenericAgent类使用SystemState对象来侦听 Pocket PC 上的所有属性更改。当属性更改时,将自动引发此类的Changed事件。

public class GenericAgent
{
    public delegate void ChangedDelegate(object sender, 
                         ChangeEventArgs args);
    public event ChangedDelegate Changed;

    List<systemstate> stateList = new List<systemstate>();
    public GenericAgent()
    {
        for (int i = 0; i <= 140; i++)
        {
            SystemState s = new SystemState((SystemProperty)i);
            s.Changed += new ChangeEventHandler(changedevent);
            stateList.Add(s);
        }
    }

    void changedevent(object sender, ChangeEventArgs args)
    {
        if (Changed != null)
        {
            Changed(sender, args);
        }
    }
}

现在,让我们预览一下Subscription类,该类保留用户订阅信息,例如事件参数和要执行的操作。我将仅列出重要的方法,而不是在此处粘贴完整的类代码。有关完整的类信息,您可以下载源代码。

public class Subscription
{
    public enum SubscriptionTypes
    {
        EmailRecieved = 0,
        SmsRecieved = 1,
        MissedCall = 2,
        Generic = 3,
    }
    public enum ActionTypes
    {
        SendSms = 0,
        SendEmail = 1,
        Call = 2,
    }
    public Subscription(SubscriptionTypes parSubscriptionType, 
                        ActionTypes parActionType, string parNotifyTo)
    {
        _SubscriptionType = parSubscriptionType;
        _ActionType = parActionType;
        _NotifyTo = parNotifyTo;
    }
    public Subscription(SubscriptionTypes parSubscriptionType, 
           ActionTypes parActionType, SystemProperty parSystemProperty, 
           string parNotifyTo)
    {
        _SubscriptionType = parSubscriptionType;
        _ActionType = parActionType;
        _NotifyTo = parNotifyTo;
        _Property = parSystemProperty;
    }
    public void InvokeAction(string parBody)
    {
        switch (_ActionType)
        {
            case ActionTypes.SendSms:
                Helpers.SmsHelper.SendSms(parBody, _NotifyTo);
                break;
            case ActionTypes.SendEmail:
                Helpers.EmailHelper.SendEmail(parBody, _NotifyTo);
                break;
            case ActionTypes.Call:
                Helpers.TelephonyHelper.Call(_NotifyTo);
                break;
            default:
                break;
        }
    }
}

正如我之前提到的,您可以通过设置订阅类型、操作类型(引发事件时将执行的操作)、操作参数(例如电话号码或电子邮件地址)或要侦听GenericAgentSystemProperty来进行订阅。当事件引发时,从PocketPcAgent对象调用使用 helper 的InvokeAction方法。

PocketPcAgent是应用程序的主类。它携带Subscription对象的集合,并且它具有每个 Agent 类的实例,并订阅由 agent 类引发的事件。引发事件时,它会检查是否存在适当的订阅。如果找到订阅,则调用其InvokeAction方法来通知用户。

TelephonyAgent insTelephonyAgent = new TelephonyAgent();
SmsAgent insSmsAgent = new SmsAgent();
EmailAgent insEmailAgent = new EmailAgent();
GenericAgent insGenericAgent = new GenericAgent();

private bool _IsAgentRunning = false;
public PocketPcAgent()
{
    insTelephonyAgent.CallMissed += 
      new TelephonyAgent.CallMissedDelegate(insTelephonyAgent_CallMissed);
    insSmsAgent.SmsReceived += 
      new SmsAgent.SmsReceivedDelegate(insSmsAgent_SmsReceived);
    insEmailAgent.EmailReceived += 
      new EmailAgent.EmailReceivedDelegate(insEmailAgent_EmailReceived);
    insGenericAgent.Changed += 
      new GenericAgent.ChangedDelegate(insGenericAgent_Changed);
}

void insGenericAgent_Changed(object sender, 
               Microsoft.WindowsMobile.Status.ChangeEventArgs args)
{
    if (IsAgentRunning)
    {
        var result = (from s in this.Subscriptions where s.SubscriptionType == 
                      Subscription.SubscriptionTypes.Generic && 
                      s.Property == ((SystemState)sender).Property select s);
        if (result.Count() > 0)
        {
            string parBody = string.Format("value of {0} changed to :{1}", 
                             result.First().Property.ToString(), args.NewValue.ToString());

            RaiseEventCaptured(result.First().Property.ToString() + ":" + parBody);

            foreach (Subscription s in result)
            {
                s.InvokeAction(parBody);
            }
        }
    }
}

void insEmailAgent_EmailReceived(Microsoft.WindowsMobile.PocketOutlook.EmailMessage msg)
{
    if (IsAgentRunning)
    {
        string parBody = string.Format("You have received an email message " + 
                         "at your pocket pc\r\nFrom : {0}\r\nBody:{1}", 
                         msg.From, msg.BodyText);
        var result = (from s in Subscriptions where s.SubscriptionType == 
                                Subscription.SubscriptionTypes.EmailRecieved select s);
        if (result.Count() > 0)
        {
            RaiseEventCaptured("Email Agent : " + parBody);
        }
        foreach (Subscription s in result)
        {
            s.InvokeAction(parBody);
        }
    }
}

void insSmsAgent_SmsReceived(Microsoft.WindowsMobile.PocketOutlook.SmsMessage msg)
{
    if (IsAgentRunning)
    {
        string parBody = string.Format("You have received an sms message " + 
                         "at your pocket pc\r\nFrom : {0}\r\nBody:{1}", msg.From, msg.Body);
        var result = (from s in Subscriptions where s.SubscriptionType == 
                                Subscription.SubscriptionTypes.SmsRecieved select s);
        if (result.Count() > 0)
        {
            RaiseEventCaptured("SmsAgent : " + parBody);
        }
        foreach (Subscription s in result)
        {
            s.InvokeAction(parBody);
        }
    }
}

void insTelephonyAgent_CallMissed(string callerName, string callerNumber)
{
    if (IsAgentRunning)
    {
        string parBody = string.Format("You have a missed call at " + 
                         "your pocket pc\r\nCaller Name : {0}\r\nNumber:{1}", 
                         callerName, callerNumber);
        var result = (from s in Subscriptions where s.SubscriptionType == 
                                Subscription.SubscriptionTypes.MissedCall select s);
        if (result.Count() > 0)
        {
            RaiseEventCaptured("TelephonyAgent : " + parBody);
        }
        foreach (Subscription s in result)
        {
            s.InvokeAction(parBody);
        }
    }
}

除了这些功能之外,用户订阅和代理设置存储在一个文件中,该文件表示序列化的PocketPcAgent对象。PocketPcAgent 应用程序还会将引发的每个事件或执行的每个操作记录到日志文件中。

联系方式

© . All rights reserved.