Skip to content

Instantly share code, notes, and snippets.

@iniyan455
Last active September 17, 2020 16:42
Show Gist options
  • Save iniyan455/1dd58d44a709c2531bccb4ec602e3fc8 to your computer and use it in GitHub Desktop.
Save iniyan455/1dd58d44a709c2531bccb4ec602e3fc8 to your computer and use it in GitHub Desktop.
deadsystemexception-start-service-android
This crash is present on:
all Samsung devices 7.0
all Nexus devices on 7.1.2
When the device is being restarted, you cannot run an app. Any app, not just yours but any given app cannot run when the device is restarting. AFAIK it occurs when you try to do something when the system crashes.
Meaning if the system crashes and as it restarts your app starts a service or does something you get that error.
Meaning the exception is not connected to your app, but the Android OS and there is nothing you can do about it.
The crash being related to starting a service is because that is what your app did when the system crashed.
So: The error is something the system threw because your app did something when the system did a run-time reboot. There is nothing you can do about this, as you cannot control the Android OS from an app.
The core Android system has died and is going through a runtime restart. All running apps will be promptly killed.
Note : -
This means that your service had already stopped - either killed from the OS, or stopped from your application.
Does this problem happen every time you debug your project?
Override your service's onDestroy() method and watch what event flow leads to it. If you catch DeadObjectException without going through this method, your service should have been killed by the OS.
From line 8 notificationManager.createNotificationChannel(channel) - create notification in above sdk 26 plus
The Android Developer docs for DeadSystemException says the following:
The core Android system has died and is going through a runtime restart. All running apps will be promptly killed.
createNotification() method it will throw a DeadSystemException.
After a deeper look, I have noticed that there is another exception being thrown before DeadSystemException, an ArrayOutOfBoundsException. It comes from the NotificationManager class. When tracking the reason of the exception It turned out that setVibrationPattern() the main factor for all these exceptions.
channel.setVibrationPattern(new long[0]);
https://proandroiddev.com/oreo-notification-feature-a-critical-issue-that-could-restart-your-android-phone-ca122fa4d9cb
If you look carefully into the method, you can identify that the method can leads for ArrayOutOfBoundException if we are sending an array with a zero size at line 7, when this happens the system throws DeadSystemException and restart the phone.
How to prevent the issue?
To prevent the issue simply don’t use the setVibrationPattern() if you still wanna use it, you can by sending a null.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Default", NotificationManager.IMPORTANCE_DEFAULT);
channel.setVibrationPattern(new long[0]);
channel.enableVibration(false);
channel.setSound(null,null);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment