Android version: Android 10
If TestService is not defined in the "AndroidManifest.xml", then we can't use "context.startService(new Intent(context, TestService.class))" to start the service.
It should be related to the service registration.
Case Study: I want to load a jar at runtime, and that jar contains some services. I don't want to define all services inside the "AndroidManifest.xml".
Question 1: Is it possible to register the TestService programmatically before starting it? If yes, how to do?
Question 2: If dynamic service registration is impossible, then why can Android dynamically register the service which defined in AndroidManifest.xml?
AndroidManifest.xml
<application
...
<!--
<service
android:name=".service.TestService"/>
-->
...
</application>
TestService.java
public class TestService extends IntentService {
...
protected void onHandleIntent(@Nullable Intent intent) {
System.out.println("------ TestService onHandleIntent ------");
}
...
}
TestActivity.java
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
test(getContext());
}
});
public static void test(Context context) {
context.startService(new Intent(context, TestService.class));
}
Thanks.
Check here to get more detail https://developer.android.com/guide/topics/manifest/manifest-intro#components
For each app component that you create in your app, you must declare a corresponding XML element in the manifest file: If you subclass any of these components without declaring it in the manifest file, the system cannot start it.
Basiclly, when you start any component android start it by android system not inside your application. Additional android support start activity or bind service from other applicationa so you must declare in AndroidManifest to tell other apps