Reputation: 1036
I have encountered huge amount of exceptions reported from Playstore. RemoteServiceException from Android P.
I am creating android foreground service and then calling startForegound(with channel).
But 99.9% only 9.0(android P) users report RemoteServiceException. I checked whether i make notification channel for the service. I also checked whether i call startForegroundService for os after OREO.
Every code had no problem.
But i found that i am calling startForegroundService() multiple times, and but onCreate() of Service called only once at the first time when it is created. So startForeground() inside of onCreate() is called only once.
But if i put startForeground() in the onStartCommand(), then it will also called as many times as i call startForegroundService(). Because it will be also called whenever you call startService/startForegroundService (even though the instance of Service is already made).
Do you think it is the cause of the Exception.
And mboy's comment of https://stackoverflow.com/a/51251741/5343 also says similar things.
Upvotes: 12
Views: 3024
Reputation: 819
Same problem here. In my case, when I change id, seem to fix this.
before
startForeground(1, notification);
after
startForeground(100001, notification);
Upvotes: 0
Reputation: 21
You can try this code block before call startForeground()
in onStartCommand()
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotifyManagerUtils.getNotificationDefaultChannelId());
builder.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.app_icon);
startForeground(YOUR_ID, builder.build());
}
} catch (Exception e) {
if (DEBUG) {
Log.e(TAG, e.getMessage());
}
}
More informations you should know in Service onCreate()
just called once if you startService()/startForegroundService()
many times but onStartCommand()
will run as same as you called times, so I think you should add a check for ur Service if this Service alive so don't call startForeground()
maybe can solve this problem.
Same problem please check this.
Upvotes: 0