1
I have a problem to start a service on android soon when the phone turns on I followed several tutorials but always causes this error
06-04 23:56:27.305 10948-10948/pluswallpapers.devmarques.com.wallpapersall E/Androidruntime: FATAL EXCEPTION: main Process: pluswallpapers.devmarques.com.wallpapersall, PID: 10948 java.lang.Runtimeexception: Unable to start receiver pluswallpapers.devmarques.com.wallpapersall.Youwillneverdestroy.Startserviceboot: java.lang.Illegalstateexception: Not allowed to start service Intent { flg=0x10000000 cmp=pluswallpapers.devmarques.com.wallpapersall/.YouWillNeverDestroy.MyService }: app is in background uid UidRecord{2368819 u0a298 RCVR idle change:uncached procs:1 seq(0,0,0)} at android.app.Activitythread.handleReceiver(Activitythread.java:3216) at android.app.Activitythread. -wrap17(Unknown Source:0) at android.app.Activitythread$H.handleMessage(Activitythread.java:1688) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.Activitythread.main(Activitythread.java:6520) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Runtimeinit$Methodandargscaller.run(Runtimeinit.java:440) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:807) Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { flg=0x10000000 cmp=pluswallpapers.devmarques.com.wallpapersall/.YouWillNeverDestroy.MyService }: app is in background uid UidRecord{2368819 u0a298 RCVR idle change:uncached procs:1 seq(0,0,0)} at android.app.Contextimpl.startServiceCommon(Contextimpl.java:1521) at android.app.Contextimpl.startService(Contextimpl.java:1477) at android.content.Contextwrapper.startService(Contextwrapper.java:650) at android.content.Contextwrapper.startService(Contextwrapper.java:650) at pluswallpapers.devmarques.com.wallpapersall.Youwillneverdestroy.StartServiceBoot.onReceive(Startserviceboot.java:15) at android.app.Activitythread.handleReceiver(Activitythread.java:3209) at android.app.Activitythread. -wrap17(Unknown Source:0) at android.app.Activitythread$H.handleMessage(Activitythread.java:1688) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.Activitythread.main(Activitythread.java:6520) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Runtimeinit$Methodandargscaller.run(Runtimeinit.java:440) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:807)
The start boot class is this, it has everything declared right there in the manifest, the service, the startboot completed, I believe it can be something to initialize the service in this broadcast class but I do not know how to solve...
public class StartServiceBoot extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent);
}
}
}
recalling that it is not an Activity only a service in the background, if the service is started by an Activity works normally, I believe that I must be infringing something of android security not to boot in the broadcast.
It worked perfectly, I had no idea that this would be the problem, @Ramaral as I’ve seen you responding a lot related to android, and I see that you have understanding, you would have some site that explains well how the service works, the execution of it in the background?
– Steve Rogers
Start with the documentation, it will give you an overview. When understanding the principles it will be easy for you to then know how to search/ask for more specific aspects.
– ramaral
For services started in the manifest, for example... <service android:name="com.parse.fcm.Parsefirebaseinstanceidservice" android:Exported="true"> <Intent-filter> <action android:=name"com.google.firebase.INSTANCE_ID_EVENT" /> </Intent-filter> </service> how can I set to be in foreground?
– Mateus Carvalho
"For services initiated in the manifest" - There is no such thing. The services are initiated by an application or service.
– ramaral