Guys, there is a task, I do not know how to approach. In the override onHandleIntent method there is a try catch , in which I need to shove the output of AlertDialog , I don’t know how to do this correctly, since there is no interface here (+ you’ll have to knock on the activation)?

Class code

 class GcmRegistrationIntentService : IntentService("GcmRegistrationIntentService") { override fun onHandleIntent(intent: Intent?) { try { val gcmSenderId = FlavorSettings.gcmSenderId val systemToken = if (gcmSenderId.isEmpty()) gcmSenderId else InstanceID.getInstance(this).getToken(gcmSenderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null) if (PreferenceRepository.gcmToken != systemToken) { PreferenceRepository.gcmToken = systemToken if (UserSession.isUserSignedIn() && ProfilesRepository.lastProfile() != null) { JobManager.scheduleAutoSync() } } } catch (e: Exception) { e.log() } } 

}

  • Add your code and describe the problem in more detail. What interface and where not? - eugeneek
  • @eugeneek updated the question. - Inkognito
  • To show the AlertDialog from the IntentService it must have the type TYPE_SYSTEM_ALERT , and for this you need the appropriate perm. Describe your usage scenario. Perhaps you do not need a dialogue and something else is suitable, for example, notification. - eugeneek
  • @eugeneek, the fact is that when I catch an error, I must display the dialogue to the user. But how to put it here a little hard. - Inkognito

2 answers 2

And the idea from the commentary is actually quite good.

In your service, knock using EventBus'a class:

 EventBus.getDefault().post(ErrorEvent()) 

In the ErrorEvent class itself, add the required fields depending on your needs.

Well, then, the place (fragment / activation) where you want to place your AllertDialog in the onEvent method, passing the arguments of this empty class to it (you should also add a ThreadMode.MAIN to the annotation):

 @Subscribe(threadMode = ThreadMode.MAIN) fun onEvent(event: ErrorEvent) { //some functions ... } 

Well, do not forget to implement in this class in the onStart method:

 EventBus.getDefault().register(this) 

And in the onStop method:

 EventBus.getDefault().unregister(this) 

Upd :

Since Android by default builds the work of the application in one process, you should not worry about their work in different processes. If you are not sure, then look in AndroidManifest.xml and review the process attribute in your application and make sure that there are no differences.

  • I do not know why, but the onEvent method does not start ( - Inkognito
  • @Inkognito and before that all the steps pass? Tried to debug, catch logs? - Morozov
  • Thank you, called in the wrong activit is simple. - Inkognito

The output of the dialog from the Service is antipattern. Service is by definition something that should work in the background and it is natural to try to bring something from it to the UI level, to put it mildly, it is doubtful.

Recommended options:

  1. Send Broadcast, which will be caught somewhere and processed
  2. Display notification or toast
  3. Use the standard pattern of interaction of the service with the Activity and process the dialogue in the Activity .

But if absolutely it is gone in, then look life hack offered in Habré

Update

Regarding the hacks with EventBus/otto/rxJava will answer with just one quote :

A common misunderstanding about Android multitasking. In Android, it is currently running the app; may be used for multiple processes; it can be kept around Android, even when that application is not actively doing something.

In free translation, this means that it is impossible to guarantee that the application and service will always be within the same process.

If this is the case, then probably a bunch of Activity-Service via EventBus will EventBus work, but this is probably not quite what we want, is it?

  • And if you go a little different way, from the service try to knock on activations with the help of EventBus and in the activites already implement the dialogue? - Inkognito
  • one
    Yes, they pulled up their EventBus/Otto/rxJava - from the service, in general, it does not work ( Service and Activity may be in different processes) - Barmaley
  • @Barmaley And if you use one activity, then I wonder how the service can be in another process? - Morozov
  • Elementary Watson - specifying the named attribute android:process - Barmaley
  • @Barmaley then I do not understand what the joke of "not love" for EventBus is, for service and activation you prescribe using one process and voila. And by default, Android throws everything in 1 process. - Morozov