2010-06-24 11 views
18

मेरे पास एक सेवा चल रही है जो अधिसूचना बार में अधिसूचना अद्यतन करती है जब यह एक संदेश प्राप्त करता है जिसमें कहा जाता है कि इसे बदला जाना है।एंड्रॉइड - java.lang.IllegalArgumentException: contentIntent अधिसूचना के कारण त्रुटि की आवश्यकता है?

चर सेटअप


int icon = R.drawable.notification; 
CharSequence tickerText = "Test"; 
long when = System.currentTimeMillis(); 
PendingIntent contentIntent; 

Notification notification = new Notification(icon, tickerText, when); 

NotificationManager mNotificationManager; 

NotificationManager:

हालांकि मैं कभी कभी जब अधिसूचना

java.lang.IllegalArgumentException: contentIntent required 

यहाँ अद्यतन किया जाना है निम्नलिखित त्रुटि मिलती है मेरी कोड है सृष्टि


String ns = Context.NOTIFICATION_SERVICE; 
    mNotificationManager = (NotificationManager) getSystemService(ns); 

अधिसूचना निर्माण


Intent notificationIntent = new Intent(this, TestsApp.class); 
    contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
    notification.flags |= Notification.FLAG_NO_CLEAR; 
    notification.icon = R.drawable.notification3; 
    notification.setLatestEventInfo(this, "Registering", "Test", contentIntent); 
    mNotificationManager.notify(1, notification); 

अधिसूचना का अद्यतन


notification.icon = R.drawable.notification2; 
    notification.setLatestEventInfo(getApplicationContext(), "Registered", "Test", contentIntent); 
    mNotificationManager.notify(1, notification); 

तो कुछ रेखा के साथ कहीं न कहीं मेरे contentIntent हो रहा है, क्या यह सही हो सकता है?

यह सदस्य सेवा चर के रूप में मेरी सेवा कक्षा के शीर्ष पर घोषित किया गया है और उपरोक्त दिखाए गए कोड में कहीं और नहीं उपयोग किया जाता है, तो यह शून्य पर रीसेट हो सकता है?

उत्तर

15

आपको अपनी सूचना के लिए सामग्री इन्टेंट सेट करने की आवश्यकता है।

अपने मामले में

:

notification.contentIntent = notificationIntent; 

अन्यथा आपको लगता है कि अधिसूचना की contentIntent, अशक्त है, क्योंकि यह स्थापित नहीं किया गया, संदेश मिल जाएगा।

दस्तावेज यहाँ है: http://developer.android.com/reference/android/app/Notification.html#contentIntent

मैं एक छोटे से उदाहरण यहाँ है: http://united-coders.com/nico-heid/show-progressbar-in-notification-area-like-google-does-when-downloading-from-android

+5

Android के सभी संस्करणों में यह आवश्यकता होती है। मेरे पास ऐसा मामला था जो किंडल फायर को छोड़कर सब कुछ ठीक काम कर रहा था। –

+4

क्या आप जानते हैं कि अगर मैं अधिसूचना पर क्लिक करने के लिए कोई इरादा नहीं चाहता हूं तो मैं क्या करूँ? मैं बस वहां रहना चाहता हूं जबकि सिस्टम कुछ कर रहा है और यह स्वयं ही जाएगा। मेरे मामले के लिए –

+0

, सेवा से notificationBar दिखा रहा है, कि mBuilder.setContentIntent (PendingIntent.getActivity (यह, 0, नई आशय(), 0)) हो सकता है; – toantran

2

अपने मामले

contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

में आप एक ही कार्रवाई लेकिन विभिन्न अतिरिक्त के साथ इंटेंट उपयोग करना चाहते हैं:

1)

012 बदलें डिफ़ॉल्ट "0" से

getActivity (Context context, int requestCode, Intent intent, int flags)

`तरह

(int) System.currentTimeMillis();

` 2)

notification.contentIntent = notificationIntent;

दोनों चरणों कुछ अद्वितीय को

में हैं अनिवार्य है क्योंकि:

  • विकल्प 2 1.
  • विकल्प 1 IllegalArgumentException फेंक होगा बिना 2.
0

मेरे मामले में विकल्प के बिना काम नहीं करेगा, मैं एक ही अधिसूचना बनाने के लिए साथ, ऐसा करने के लिए एक उदाहरण कोड था, और मैं "सामग्रीइन्टेंट आवश्यक" त्रुटि भी मिली - Google ने मुझे इस धागे पर लाया: डी

इस समस्या का स्रोत उद्धरण था जो मैंने एक उदाहरण कोड से कॉपी किया और इसे ग्रहण परियोजना में चिपकाया। जब मैंने हटा दिया और उन्हें वापस टाइप किया और समस्या हल हो गई। शायद यह किसी की मदद करता है।

ये त्रुटि के उद्धरण स्रोत थे: nb.setContentTitle ("मेरी पहली अधिसूचना!"); nb.setContentText ("हैलो");

3

मुझे लगता है कि इस वजह से Android OS वर्शन

संस्करण 2.3 या कम,, contentIntent सेट यदि नहीं, तो आप इस अपवाद मिल जाएगा चाहिए।

मेरी परियोजना में, मैं इस तरह लिखना:

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { Intent intent = new Intent(); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); mNotification.contentIntent = contentIntent; }

शायद यह आपकी मदद कर सकता है!

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