How to show popup message without context

We are developing a library that will be used in both the JVM and Android . And now we need to implement a trial notice. On other platforms (.Net and Mac) showing a popup message box was enough. But so far I can’t find a way to do this for Android.

The problem is that to display some message (toast or AlertDialog) I need a real context. But since our library does not contain user interface elements and is not associated with the user interface, we do not ask the user to pass context when creating components.

In short:

  • how can I show Toast (AlertDialog, Notification) without having a contextual response passed from user code.
  • If I can’t, maybe someone has the best solutions for implementing trial mode on Android and Java.
+7
source share
2 answers

It sounds like you really can't show either Toast or AlertDialog without context. The closest option is to provide some interface in your library that the user must implement.

+1
source

Use the abstract factory template .

Your factory abstract will allow you to create dialogs and other notifications. Then you will have 2 specific implementations that will create an Android version or a JVM notification version.

Example:

 /* * The abstract factory */ public abstract class NotificationFactory { /** * Pops a message up for the user. * * @param msg */ public abstract void newToast(String msg); // other notifications can go here } /** * Concrete Android notification factory * */ public final class AndroidNotificationFactory extends NotificationFactory { private final Context context; public AndroidNotificationFactory(Context context) { this.context = context; } @Override public void newToast(String msg) { // normal Android toast stuff using the context } } /** * Concrete JVM Notification factory * */ public final class JvmNotificationFactory extends NotificationFactory { @Override public void newToast(String msg) { // JVM toast stuff } } /** * A singleton used for accessing the factory. * */ public enum Library { Instance; private NotificationFactory notificationFactory; public NotificationFactory getNotificationFactory() { // nb: add synchronized if needed return notificationFactory; } public void setNotificationFactory(NotificationFactory notificationFactory) { // nb: add synchronized if needed this.notificationFactory = notificationFactory; } public void InitForAndroid(Context context) { setNotificationFactory(new AndroidNotificationFactory(context)); } public void InitForJvm() { setNotificationFactory(new JvmNotificationFactory()); } } public class ExampleUsage { /** * @param args */ public static void main(String[] args) { // init the library // Library.Instance.InitForAndroid(context); //or: Library.Instance.InitForJvm(); // usage Library.Instance.getNotificationFactory().newToast("Test"); } } 

With dialog boxes where you will need to call methods after creation, you need to create the IDialog interface and the specific Android and JVM shells that implement this interface.

+3
source

All Articles