-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathService.txt
44 lines (35 loc) · 1.13 KB
/
Service.txt
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
1-- define service
[Service]
public class DemoIntentService : IntentService
{
public DemoIntentService() : base("DemoIntentService")
{
}
protected override void OnHandleIntent(Android.Content.Intent intent)
{
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
// start a task here
new Task(() =>
{
// long running code
while (true)
{
Intent intents = new Intent();
intents.SetAction("com.alr.text");
intents.PutExtra("MyData", "Data from Activity1");
SendBroadcast(intents);
System.Threading.Thread.Sleep(1000);
}
}).Start();
return StartCommandResult.Sticky;
}
}
2-- start service
this.StartService(new Intent(this, typeof(DemoIntentService)));
3-- register service if needed
<service
android:name=".DemoIntentService"
android:exported="false" >
</service>