2012-06-25 15 views
7

हम एक पुस्तकालय विकसित कर रहे हैं जिसका उपयोग JVM और Android दोनों में किया जाएगा। और अब हमें परीक्षण अवधि अधिसूचना लागू करने की आवश्यकता है। अन्य प्लेटफ़ॉर्म (.Net और Mac) में पॉपअप संदेश बॉक्स दिखा रहा था। लेकिन अब तक मुझे एंड्रॉइड के लिए ऐसा करने का कोई तरीका नहीं मिल रहा है।संदर्भ के बिना पॉपअप संदेश कैसे दिखाएं

समस्या यह है कि कुछ संदेश (टोस्ट या अलर्टडिअलॉग) दिखाने के लिए, मुझे एक वास्तविक संदर्भ होना चाहिए। लेकिन चूंकि हमारी लाइब्रेरी में यूआई तत्व नहीं हैं और यूआई से संबंधित नहीं हैं, इसलिए हम किसी घटक को घटक बनाते समय संदर्भ पारित करने के लिए नहीं कह रहे हैं।

कम होने के लिए:

  • कैसे मैं एक संदर्भ संदर्भ उपयोगकर्ता कोड से पारित किए बिना एक टोस्ट (AlertDialog, अधिसूचना) दिखा सकते हैं।
  • यदि मैं शायद एंड्रॉइड और जावा पर परीक्षण मोड को लागू करने के लिए बेहतर समाधान नहीं कर सकता।
+1

आप बिना किसी संदर्भ के कुछ भी कर सकते हैं। हो सकता है कि अधिसूचनाओं के लिए एक इंटरफ़ेस प्रदान करें और उपयोगकर्ता को इस इंटरफ़ेस को लागू करने दें –

उत्तर

1

ऐसा प्रतीत होता है कि आप वास्तव में संदर्भ के बिना न तो टोस्ट और न ही अलर्टडिअलॉग नहीं दिखा सकते हैं। सबसे करीबी संस्करण आपकी लाइब्रेरी में कुछ इंटरफ़ेस प्रदान करना है जिसे उपयोगकर्ता को कार्यान्वित करना चाहिए।

3

Abstract factory pattern का उपयोग करें।

आपका सार कारखाना आपको संवाद और अन्य सूचनाएं बनाने की अनुमति देगा। फिर आपके पास 2 ठोस कार्यान्वयन होंगे जो एंड्रॉइड संस्करण या अधिसूचना के JVM संस्करण को बनाएंगे।

उदाहरण:

/* 
* 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"); 

    } 

} 

संवाद जहां पर निर्माण के बाद तरीकों कॉल करने के लिए सक्षम होना चाहिए साथ, आप एक IDialog इंटरफेस और ठोस Android और JVM रैपर कि कि इंटरफ़ेस को लागू करने की जरूरत है।

+0

धन्यवाद, यह एक अच्छा उदाहरण है कि प्लेटफॉर्म-स्वतंत्र तरीके से नोटिफिकेशन कैसे दिखाएं। लेकिन सवाल का सार यह है कि मेरी लाइब्रेरी से यूआई दिखाने के लिए कैसे मजबूर होना है। और अब ऐसा लगता है कि मैं संदर्भ के संदर्भ के बिना ऐसा नहीं कर सकता। – Antonio

संबंधित मुद्दे