The task is this. There is a service that runs continuously, even when all activations of the application are closed. It sends data to the application class, which is not destroyed when activating is closed. This class is waiting for messages from the service. And when the message arrives, it must initiate the opening of a dialogue that is not tied to any activity. Is it possible and how to do it? Or maybe the service itself can create a dialogue?

  • Do you need a dialogue or just a view also suitable? - Yuriy SPb
  • In extreme cases, you can view, and how to show it without activating? I think the dialogue is easier. - Helena2977
  • one
    You can launch the activation with the theme of the dialogue . That is, if you start the activation, it will look like a dialog box on the screen. - pavlofff
  • And how to start activating from a simple class? Naspimer, if I save the link to Application there, can I activate it from there? - Helena2977

2 answers 2

You can display the view from the service as follows:

  1. We receive object-screen in which we will add views
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); 
  1. Add to it programmatically created addView(View v, LayoutParams params) by the addView(View v, LayoutParams params) method addView(View v, LayoutParams params)
 windowManager.addView(view, params); 
  1. In the manifest, specify the desired resolution:
 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 
  • And where to get an inflater to create views? Does any of the old activites, which already can not? - Helena2977
  • And what to set as a params? - Helena2977
  • inflater just from the context take LayoutInflater.from(context) and the parameters ... Probably any, of the type new LinearLayoutParams(100, 100) - Yurii

As an option, you can make a transparent Activity with a style like:

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme.Transparent" parent="android:Theme"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:backgroundDimEnabled">false</item> </style> </resources> 

from which already "give birth" Dialog

  • If the activation, then how to start it from the class that does not activate? - Helena2977
  • To start an Activity , you only need the Context from which the Intent starts. Service has context so no problem in starting activation from service does not exist - Barmaley
  • In this example you will find the answer. It implemented a floating button on the desktop as in the fb messenger. github.com/trinhlbk1991/DemoAndroidFloatingView - ivansoft