Reputation: 11
I'm not a Windows Phone developer myself, so I apologize in advance for my lack of knowledge.
One of the products we offer our customers is a caller ID app. The software does the following:
We've implemented the app on Android and Symbian and it works perfectly. I'd like to know if this type of application is even possible on a Windows Phone. According to our subcontractor, this hasn't been the case with previous versions. I'd like to know if this is really true and if things have changed in WP8.
My questions:
From what I've googled, I've only found mentions about the "Obscured" event, nothing about a "Call" event or similar. This isn't very promising, but I'd like to hear from actual developers.
Upvotes: 1
Views: 1664
Reputation: 340
Yes (look up "Background Agents," http://www.jeffblankenburg.com/2011/11/25/31-days-of-mango-day-25-background-agents/).
You should start there and then perform more independent research related to the spec of your app.
Per Jeff's article:
Launch Visual Studio and create a new project. Under Silverlight for Windows Phone, select Windows Phone Application. Name it “MyAgentApp”.
You’ve now created the main application. It will be responsible for two things:
1) Having a live tile that the Background Agent can update with information
2) Starting and stopping the Background Agent
The Background Agent itself must live in its own special project. Add a new project to your solution, selecting Windows Phone Scheduled Task Agent. Name it MyAgent. This project will contain your custom code that will run in the background and update the live tile.
Finally, and this is important, go to the MyAgentApp project and add a project reference to MyAgent. This will allow you to register your agent from within the application. Also, notice the entry this automatically created in WMAppManifest.xml:
<Tasks>
<DefaultTask Name="_default" NavigationPage="MainPage.xaml" />
<ExtendedTask Name="BackgroundTask">
<BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="MyAgent" Source="MyAgent" Type="MyAgent.ScheduledAgent" />
</ExtendedTask>
</Tasks>
Now it’s time to actually wire everything up. Open MainPage.xaml and add two buttons, one for starting the agent, and the other for stopping the agent:
<StackPanel VerticalAlignment="Center">
<Button Content="Start Background Agent"
Click="StartButton_Click"/>
<Button Content="Stop Background Agent"
Click="StopButton_Click"/>
</StackPanel>
In MainPage.xaml.cs, wire up the buttons to start and stop the agent:
private const string TASK_NAME = "MyAgent";
private void StartButton_Click(object sender, RoutedEventArgs e)
{
StartAgent();
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
StopAgentIfStarted();
}
private void StartAgent()
{
StopAgentIfStarted();
PeriodicTask task = new PeriodicTask(TASK_NAME);
task.Description = "This is our custom agent for Day 25 - Background Agents";
ScheduledActionService.Add(task);
#if DEBUG
// If we're debugging, attempt to start the task immediately
ScheduledActionService.LaunchForTest(TASK_NAME, new TimeSpan(0, 0, 1));
#endif
}
private void StopAgentIfStarted()
{
if (ScheduledActionService.Find(TASK_NAME) != null)
{
ScheduledActionService.Remove(TASK_NAME);
}
}
Notice that to create our custom agent, we are creating a new PeriodicTask. We then use the name as an identifier when finding and stopping the agent. Notice also that we specified PeriodicTask.Description – this is a required field and will appear in Settings | Background Tasks under the name of our application.
In the MyAgent project, open ScheduledAgent.cs and add the following code:
protected override void OnInvoke(ScheduledTask task)
{
UpdateAppTile(GetLastUpdatedTimeMessage());
}
private string GetLastUpdatedTimeMessage()
{
return string.Format("Last Updated: {0}", DateTime.Now);
}
private void UpdateAppTile(string message)
{
ShellTile appTile = ShellTile.ActiveTiles.First();
if (appTile != null)
{
StandardTileData tileData = new StandardTileData
{
BackContent = message
};
appTile.Update(tileData);
}
}
ScheduledAgent has one important method to override – OnInvoke. This is where your agent will execute its background task. If your task is complete and you no longer need your agent to run, you can call NotifyComplete() to signal that the task completed successfully or Abort() to signal that you are cancelling your task. To keep the task running at an interval, simply do not call either, which is what we are doing here.
Upvotes: 1