Runtime exception Android O with boot_completed
Here are some options that I outlined in a blog post:
Workaround #1: startForegroundService()
Your BroadcastReceiver
that receives the ACTION_BOOT_COMPLETED
broadcast
could call startForegroundService()
instead of startService()
when on Android
8.0+:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
public class OnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i=new Intent(context, TestIntentService.class);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
context.startForegroundService(i);
}
else {
context.startService(i);
}
}
}
Note that this works, to an extent, even if your service does not actually
ever call startForeground()
. You are given a window of time to get around
to calling startForeground()
, "comparable to the ANR interval to do this".
If your work is longer than a millisecond but less than a few seconds,
you could skip the Notification
and the startForeground()
call. However,
you will get an error in LogCat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.commonsware.myapplication, PID: 5991
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1775)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Of course, if you do not mind having a Notification
briefly, you are welcome
to use startForeground()
as Android expects you to, in which case you can
do background work normally, albeit with an entry showing up in the user's notification
shade.
Workaround #2: goAsync()
BroadcastReceiver
has offered goAsync()
since API Level 11. This allows your
receiver to do work off the main application thread, so you could get rid of theIntentService
entirely and move your code into the BroadcastReceiver
.
You still only have the ANR
timeout period to work with, but you will not be tying up your main application
thread. This is better than the first workaround, insofar as it has the same
time limitation but avoids the nasty error. However, it does require some amount
of rework.
Workaround #3: JobScheduler
If your work will take more than a few seconds and you want to avoid theNotification
, you could modify your code to implement a JobService
and
work with JobScheduler
. This has the added advantage of only giving you
control when other criteria are met (e.g., there is a usable Internet
connection). However, not only does this require a rewrite, but JobScheduler
is only available on Android 5.0+, so if your minSdkVersion
is less than 21,
you will need some other solution on the older devices.
UPDATE: Eugen Pechanec pointed out JobIntentService
,
which is an interesting JobService
/IntentService
mashup.
Android 8.0 - BroadcastReceiver not receiving after a reboot
Starting from Android O, you can not start a service from a background app without getting an exception:
java.lang.IllegalStateException: Not allowed to start service Intent
(my_service) : app is in background
If you still need to launch a service at device start up, you can now use the new JobIntentService.
Change your IntentService
to a JobIntentService
:
public class ReactivationService extends JobIntentService {
public static final int JOB_ID = 0x01;
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, ReactivationService.class, JOB_ID, work);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
// do your stuff here
}
}
Use it as follow in your StartupReceiver
:
public class StartupReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
ReactivationService.enqueueWork(context, new Intent());
}
}
}
Declare your service inside the manifest:
<service android:name=".ReactivationService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
And that’s it. This will either directly start the service (when running on pre-O platforms) or enqueue work for it as a job (when running on O and later). No matter what the platform is, everything you pass in enqueueWork
will ultimately appears in onHandleWork
.
xamarin boot_completed crash android 8+
I finally solved my problem. the issue was caused by firebase, who's got an update who avoid to set an boot completed service. So my fix was to not start the service for any android version up from marshmallow.
How To Test BOOT_COMPLETED Broadcast Receiver In Emulator
There is no Power Button in Emulator like Devices have,So
To stop an emulator instance, just close the emulator's window.
And To Start/Restart
it Start from AVD Manager of Eclipse and Your BroadcastReceiver with BOOT_COMPLETE action will get called for sure
You can start AVD another way also, From CMD
go to Tools of AndroidSDK and give this commmand E:\android-sdk-windows\tools>emulator -avd AVDNAMEHERE
To Send Broadcast from CMD you can use this Command.
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
Read more about Android Emulator : Android Emulator and Using Emulator
Related Topics
Activitymanager.Getrunningtasks Is Deprecated Android
Asynctaskloader Basic Example. (Android)
How to Set Ringtone with Ringtonemanager.Action_Ringtone_Picker
How to Find Memory Leak Class/Activity in Android
Difference Between Getview & Getdropdownview in Spinneradapter
How to Make Part of The Text Bold in Android at Runtime
Actionlayout on Menuitem Does Nothing
How to Import an Existing Project from Github into Android Studio
Gradle Android Build for Different Processor Architectures
How to Overlap Items in Linearlayoutmanager - Recyclerview (Like Stacking Cards)
How to Get a View from an Event Coordinates in Android
Adding a Vertical Scrollbar to an Alertdialog in Android
Fragment Standard Transition Not Animating
Android Service Startservice() and Bindservice()
Constraintlayout Chains and Text Ellipsis + Image on The Right