我在窗口手机8中创建了一个应用程序,可以从后台任务更改锁屏图像。在运行代码时,该应用程序在模拟器和设备中运行良好。它包含launchforTest(),它每30秒更换一次锁屏。
我收到一些评论告诉说,当应用程序通过商店上传时,launchforTest()不起作用。它主要用于测试目的。
所以我的问题是,当我从商店安装我的应用程序时,我已经进行了beta测试,后台任务不起作用,我的锁屏图像不会改变
我在代码中遗漏了哪些内容使其无效。请帮助。
这是我的代码
private ResourceIntensiveTask resourceIntensiveTask; private string 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.
}
}
答案 0 :(得分:0)
您的问题是您已将后台代理实施为ResourceIntensiveTask
。 ResourceIntensiveTasks在以下非常具体的约束条件下运行:
考虑将此后台代理实现为PeriodicTask。如果您遇到运行时约束,请考虑尽可能多地重构代码,以便在主应用程序运行时预先处理锁定屏幕图像的哪些部分,或者在可以使用的包中创建静态图像资源传达相同的信息。
参考: Background agents for Windows Phone: Constraints for Resource-intensive Agents