Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I created an app in window phone 8 which could change the lockscreen images from background task. The app worked well in emulator and device when run through code. It contains the launchforTest() which makes lockscreen change every 30 sec.

I got some comments that told that launchforTest() don't work when app gets uploaded over store. its basically for the testing purpose.

So my problem is that When i install my app from the store which I have put for beta testing the background tasks don't work i,e lockscreen images don't change

What am I missing in the code which has made it not working pls help.

Here is my code
private ResourceIntensiveTask resourceIntensiveTask;
    private string resourceIntensiveTaskName = "SmartLockScreenScheduledTaskAgent";
    private string resourceIntensiveTaskDescription = "Displays the latest deals.";

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.

        }


    }
Posted
Updated 22-Sep-13 18:31pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900