This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBackgroundTask.cs
55 lines (50 loc) · 1.61 KB
/
BackgroundTask.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
namespace Coolapk_UWP
{
public sealed class BackgroundTask : IBackgroundTask
{
public BackgroundTask()
{
}
public void Run(IBackgroundTaskInstance taskInstance)
{
Debug.Write("task run");
}
public static async Task<BackgroundTaskRegistration> RegisterBackgroundTask(Type taskEntryPoint,
string taskName,
IBackgroundTrigger trigger,
IBackgroundCondition condition)
{
var status = await BackgroundExecutionManager.RequestAccessAsync();
if (status == BackgroundAccessStatus.Unspecified || status == BackgroundAccessStatus.DeniedBySystemPolicy || status == BackgroundAccessStatus.DeniedByUser)
{
return null;
}
foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
if (cur.Value.Name == taskName)
{
cur.Value.Unregister(true);
}
}
var builder = new BackgroundTaskBuilder
{
Name = taskName,
TaskEntryPoint = taskEntryPoint.FullName,
};
builder.SetTrigger(trigger);
if (condition != null)
{
builder.AddCondition(condition);
}
BackgroundTaskRegistration task = builder.Register();
return task;
}
}
}