我在 windows phone 8 中创建了一个应用程序,它可以从后台任务更改锁屏图像。通过代码运行时,该应用程序在模拟器和设备中运行良好。它包含launchforTest(),它使锁屏每30秒改变一次。
我收到一些评论,告诉我当应用程序通过商店上传时,launchforTest() 不起作用。它基本上是为了测试目的。
所以我的问题是,当我从商店安装我的应用程序进行 beta 测试时,后台任务不起作用,即锁屏图像不会改变
我在代码中缺少什么使其无法正常工作请帮忙。
这是我的代码
私有资源密集任务资源密集任务;私有字符串 resourceIntensiveTaskName = "SmartLockScreenScheduledTaskAgent"; private string resourceIntensiveTaskDescription = "显示最新交易。";
private StartTask()
{
LockScreenChange("ms-appdata:///Local/Image0.jpg", true);
}
private async void LockScreenChange(string filePathOfTheImage, bool isAppResource)
{
if (!LockScreenManager.IsProvidedByCurrentApplication)
{
// If you're not the provider, this call will prompt the user for permission.
// Calling RequestAccessAsync from a background agent is not allowed.
await LockScreenManager.RequestAccessAsync();
}
// Only do further work if the access is granted.
if (LockScreenManager.IsProvidedByCurrentApplication)
{
// At this stage, the app is the active lock screen background provider.
// The following code example shows the new URI schema.
// ms-appdata points to the root of the local app data folder.
// ms-appx points to the Local app install folder, to reference resources bundled in the XAP package
// var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
var uri = new Uri(filePathOfTheImage, UriKind.Absolute);
// Set the lock screen background image.
LockScreen.SetImageUri(uri);
// Get the URI of the lock screen background image.
var currentImage = LockScreen.GetImageUri();
System.Diagnostics.Debug.WriteLine("The new lock screen background image is set to {0}", currentImage.ToString());
UpdatePrimaryTile();
StartResourceIntensiveAgent();// StartPeriodicAgent();//
// MessageBox.Show("Lock screen changed. Click F12 or go to lock screen.");
}
else
{
MessageBox.Show("Background can't be updated as you clicked no!!");
}
}
public void RemoveResourceIntensiveAgent()
{
try
{
if (resourceIntensiveTask != null)
{
ScheduledActionService.Remove(resourceIntensiveTaskName);
}
}
catch (Exception)
{
}
}
/// <summary>
/// Code to start the resource intensive task
/// </summary>
private void StartResourceIntensiveAgent()
{
// Variable for tracking enabled status of background agents for this app.
agentsAreEnabled = true;
resourceIntensiveTask = ScheduledActionService.Find(resourceIntensiveTaskName) as ResourceIntensiveTask;
// If the task already exists and background agents are enabled for the
// application, you must remove the task and then add it again to update
// the schedule.
if (resourceIntensiveTask != null)
{
ScheduledActionService.Remove(resourceIntensiveTaskName);
}
resourceIntensiveTask = new ResourceIntensiveTask(resourceIntensiveTaskName);
// The description is required for periodic agents. This is the string that the user
// will see in the background services Settings page on the device.
resourceIntensiveTask.Description = (resourceIntensiveTaskDescription);
// Place the call to Add in a try block in case the user has disabled agents.
try
{
ScheduledActionService.Add(resourceIntensiveTask);
//ResourceIntensiveStackPanel.DataContext = resourceIntensiveTask;
resourceIntensiveTask.ExpirationTime = DateTime.Now.AddDays(14);
// If debugging is enabled, use LaunchForTest to launch the agent in one minute.
ScheduledActionService.LaunchForTest(resourceIntensiveTaskName, TimeSpan.FromSeconds(30));
}
catch (InvalidOperationException exception)
{
if (exception.Message.Contains("BNS Error: The action is disabled"))
{
MessageBox.Show("Background agents for this application have been disabled by the user.");
agentsAreEnabled = false;
}
}
catch (SchedulerServiceException)
{
// No user action required.
}
}