Start an Android Service without the .class

android, android-intent, android-service

Solution

You can start it by name. Like this:

public static void startYourService(Context context) {
    Intent i = new Intent("com.whatever.servicename.YOUR_ACTION_NAME");
    i.setPackage(context.getPackageName());
    context.startService(i);
}

You'll need to add an intent filter to the apk with the service:

<service
    android:name="com.whatever.servicename"
    android:exported="true">
    <intent-filter>
        <action android:name="com.whatever.servicename.YOUR_ACTION_NAME" />
    </intent-filter>
</service>

Problem

I am looking for a way to start an Android-Service without the class. I found many examples like: ``` startService(new Intent(this, TheService.class)); ``` I don't want to put the service class in it and still have the service running till I stop it manually. I don't need to have the Interface of the service, because it only should open some Sockets without any parameters. The service running time should not depend on the running time of an App: The first App which needs the Service should start it and than it should run until it is stopped manually. So I can't use `BroadcastReceiver` because they live to short. Binding to a Service will lead to unbind which stopps the service(which I don't want). Is there any way to use start service without the .class of the service? Alternative: Is there a way to bind to service which does not link the lifetime to the started app?

Original source