How to show popup message without having a context

旧时模样 提交于 2019-12-13 13:01:15

问题


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

The problem is that to show some message (toast or AlertDialog), I need to have an actual context. But since our library doesn't contain UI elements and not related to UI in any way we are not asking a user to pass context when creating out components.

To be short:

  • how can I show a Toast (AlertDialog, Notification)without having a context refference passed from user code.
  • If I can't maybe someone have better solutions for implementing trial mode on Android and Java.

回答1:


Use Abstract factory pattern.

Your abstract factory will allow you to create dialogs and other notifications. Then you will have 2 concrete implementations that create the Android version or the JVM version of the notification.

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 dialogs where you need to be able to call methods on after creation, you need to make an IDialog interface and concrete Android and JVM wrappers that implement that interface.




回答2:


It appears that you really can't show neither Toast nor AlertDialog without having a context. The most close variant is to provide some interface in your library that user should implement.



来源:https://stackoverflow.com/questions/11189953/how-to-show-popup-message-without-having-a-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!