简体   繁体   中英

Windows phone 8.1 background tasks couldn't register the background task

I just downloaded the Sample from https://code.msdn.microsoft.com/windowsapps/Background-Task-Sample-9209ade9 and tried running the windows phone 8.1 project. The program runs but there is no background task that happens even when I made the changes to trigger the event. I also don't think its getting registered to run as a background task as I don't get any request to add the app to Lock screen. And I couldn't manually add the app to lock screen. Note: Using Lumia 730 with 8.1 Denim.

I also tried to write a sample program myself with silverlight 8.1 and background as RT component.

if (BackgroundTaskRegistration.AllTasks.Any(task => task.Value.Name == name))
            {
                // One register it once
                return;
            }
    var builder = new BackgroundTaskBuilder();
                var trigger = new SystemTrigger(SystemTriggerType.UserAway, false);
                builder.Name = name;
                builder.TaskEntryPoint = typeof(MyBackgroundTask.BackClass).FullName;                   builder.SetTrigger(trigger);

                await BackgroundExecutionManager.RequestAccessAsync();
                BackgroundTaskRegistration registration = builder.Register();
                registration.Completed += RegistrationOnCompleted;

Here too I'm getting an exception on the line

        BackgroundTaskRegistration registration = builder.Register();

The exception is runtime which states

> A first chance exception of type 'System.Exception' occurred in
> Project1.DLL

I found solution for this Issue:

  1. Make runtime Windows Phone 8.1 Project for Background task

Windwos电话背景任务

  1. When registering Task inside yor background task project:

     public async static void Register() { Debug.WriteLine("Registering geofence bg task"); if (!IsTaskRegistered()) { var result = await BackgroundExecutionManager.RequestAccessAsync(); var builder = new BackgroundTaskBuilder(); builder.Name = TaskName; builder.TaskEntryPoint = typeof(BackgroundTask.YourTaskName).FullName; builder.SetTrigger(new LocationTrigger(LocationTriggerType.Geofence)); try { builder.Register(); Debug.WriteLine("GeoFence Task Registered"); } catch (Exception ex) { Debug.WriteLine("GeoFence Task Failed : " + ex.Message.ToString()); } } else { } } 

Make sure you correctly add TaskEntryPoint by referencing it to background project name, it looks like this : "BackgroundTask.YourTaskName"

  1. TaskName string for builder.Name parameter should look like this:

    static string TaskName = "BackgroundTask.YourTaskName";

  2. Declare your TaskEntryPoint in appmanifest under declaration tab, here I use GeofenceTask so i checked few checkboxes needed for task:

Windwos电话背景任务2

Here you again enter EntryPoint(like TaskEntryPoint) : "BackgroundTask.YourTaskName"

  1. When You initalize task somewhere in app where you do that, You use :

    BackgroundTask.YourTaskName.Register();

It is all about entry point of task. So if you have your task under the separate project like it should be done, you must enter name of that project and name of background task class under it.

Do not use only background task class name alone for your EntryPoint, you must reference it with project name that contains that class.

Hope it helps,

You don't mention what the exception is, nor what the text accompanying it is. This makes it hard to answer.

Nevertheless, assuming your error is "Class not registered" you need to do several things:

  1. Make sure MyBackgroundTask.BackClass is a class in a Windows Runtime Component project (it can't be in your Silverlight project)
  2. Make sure you reference that project (you'll get compiler errors if you don't)
  3. Make sure you've correctly added a Background Task entry in the Package.appxmanifest designer, on the Declarations tab. Use the class name again as the Entry point

Well I tried to add BackgroundTaskDeferral inside my background task and it started working. I wonder why as this only assure's the async call. Anyways it worked for me. Thanks for the help

Same problem. Turned out my entry point parameter in Package.appxmanifest was spelled "Module:EntryPoint" rather than "Module.EntryPoint". Changing the colon to a period was all I had to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM