Created
June 29, 2020 21:03
-
-
Save glennstephens/78265ff796cef8717411bb9060741c0c to your computer and use it in GitHub Desktop.
A C# snippet example for coding an Android Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class $ServiceName$Binder : Binder | |
{ | |
public $ServiceName$ Service { get; private set; } | |
public $ServiceName$Binder($ServiceName$ service) | |
{ | |
this.Service = service; | |
} | |
} | |
class $ServiceName$Connection : Java.Lang.Object, IServiceConnection | |
{ | |
public event EventHandler<bool> ServiceConnectionChanged; | |
public $ServiceName$ Service { get; private set; } | |
public void OnServiceConnected(ComponentName name, IBinder service) | |
{ | |
$ServiceName$Binder lsBinder = service as $ServiceName$Binder; | |
if (lsBinder == null) | |
return; | |
Service = lsBinder.Service; | |
ServiceConnectionChanged?.Invoke(this, true); | |
} | |
public void OnServiceDisconnected(ComponentName name) | |
{ | |
ServiceConnectionChanged?.Invoke(this, false); | |
Service = null; | |
} | |
} | |
[Service] | |
public class $ServiceName$ : Service | |
{ | |
public override void OnCreate() | |
{ | |
base.OnCreate(); | |
} | |
public override IBinder OnBind(Intent intent) | |
{ | |
return new $ServiceName$Binder(this); | |
} | |
public override void OnDestroy() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment