Android service failed to start Unable to start service Intent U=0: not found

Android failed to start the service, prompting: Unable to start service Intent { cmp=xxx/.xxx} U=0: not found. There is nothing wrong with checking the manifest file service configuration, and there is nothing wrong with the method of starting the service.

startService(Intent(this, TestService::class.java))

Scenario: My program is a system application, running on 9.0, used as a launcher, and starts the service in application onCreate. I tried to start the service by delaying the start of the service and configuring the action and package, and the same error was reported.
Finally, it was found that the application in the manifest file was configured with the directBootAware attribute, which means that the program is allowed to start when the system is not started (unlocking phase), but there is no relevant configuration for TestService. Therefore, the service cannot be found when the program starts, and the problem can be solved by configuring the following properties:

 <service android:name=".TestService"
            android:directBootAware="true"
            android:enabled="true"/>
  • android:directBootAware: Whether to allow the system to run the service before unlocking the device, the default is false
  • android:enabled: Whether the system can instantiate the service, the default is true

Another exception occurred:
java.lang.IllegalStateException: SharedPreferences in credential encrypted storage are not available until after user is unlocked
This error will cause the program to crash, because the SharedPreferences data cannot be read before the device is unlocked. After the directBootAware attribute is configured, this exception will occur when the program is started and the system is not ready to operate the sp.

<application
        android:defaultToDeviceProtectedStorage="true"
        。。。。。/>

Configure the defaultToDeviceProtectedStorage property in the application to solve this problem.

The situation I encountered was that the second problem caused the program to crash continuously, because it was the launcher, which would be repeatedly launched by the system. As a result, the system is ready when the program actually starts normally, and there is no problem in starting the service at this time.
That is to say, the non-stop flashback leads to avoiding the bug that the service cannot be pulled up. It took a lot of time to check here, so make a record.

Guess you like

Origin blog.csdn.net/weixin_40652755/article/details/126607306