Oliver Weichhold Oliver Weichhold - 1 year ago 98
C# Question

Retrieving the component name of a service defined in another Xamarin application

I'm currently implementing an Android Service which is to be consumed by another application of a partner company. They implement the frontend GUI, we do the business logic inside the Service.

The Service is declared like this:

[IntentFilter(new[] { "PsiServiceHost" })]
public class PsiServiceHost : Service

The consuming application (optionally) starts the service and connects to it using this Intent:

serviceIntent = new Intent("PsiServiceHost");

Starting the service using ...


... results in the following log output:

08-16 10:38:17.182 3429 3429 W ContextImpl: Implicit intents with
startService are not safe: Intent { act=PsiServiceHost }

According to this answer on SO, starting Services using implicit intents is no longer possible even though it works fine in my case on Android 6.0 running on the Emulator.

Anyway, just to be safe I would like to use the recommended approach of constructing my Intent using a ComponentName. This would be no problem for code residing in the same project as the service as I could do this and be done with it:


But unless the result from the above line of can be considered to be stable and safe to be included as string in another App, I don't know how provide the consuming App with the component Name of our Service.

Answer Source

The Application with the Service:

Package Name = com.sushihangover.androidservice

[Service(Name="com.sushihangover.androidservice.MyMostAmazingService", Exported = true)]
[IntentFilter(new String[] { "myservice" }, Categories = new[] { Intent.CategoryDefault })]
public class PsiServiceHost : Service
    public override IBinder OnBind(Intent intent)
        return null;
    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        Log.Debug("SO", "MyMostAmazingService Has Been Started");
        return base.OnStartCommand(intent, flags, startId);

The other application starting MyMostAmazingService:

var intent = new Intent();
// Package name first, Service Class Name Second
intent.SetClassName("com.sushihangover.androidservice", "com.sushihangover.androidservice.MyMostAmazingService");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download