8

लघु प्रश्न:नोटिफिकेशन कॉम्पेट.बिल्डर और स्टार्टवार्ग का उपयोग कैसे करें?

मैं आदेश एक सूचना है कि सेवा के लिए इस्तेमाल किया जाएगा बनाने के लिए NotificationCompat.Builder वर्ग का उपयोग करने की कोशिश कर रहा हूँ, लेकिन किसी कारण से, मैं या तो अधिसूचना नहीं दिख रहा है, या जब सेवा को नष्ट किया जाना चाहिए (या अग्रभूमि में होने से रोकना) इसे रद्द नहीं करें।

मेरी कोड:

@Override 
public int onStartCommand(final Intent intent, final int flags, final int startId) { 
    final String action = intent == null ? null : intent.getAction(); 
    Log.d("APP", "service action:" + action); 
    if (ACTION_ENABLE_STICKING.equals(action)) { 
     final NotificationCompat.Builder builder = new Builder(this); 
     builder.setSmallIcon(R.drawable.ic_launcher); 
     builder.setContentTitle("content title"); 
     builder.setTicker("ticker"); 
     builder.setContentText("content text"); 
     final Intent notificationIntent = new Intent(this, FakeActivity.class); 
     final PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     builder.setContentIntent(pi); 
     final Notification notification = builder.build(); 
     // notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; 
     // notification.flags |= Notification.FLAG_NO_CLEAR; 
     // notification.flags |= Notification.FLAG_ONGOING_EVENT; 

     startForeground(NOTIFICATION_ID, notification); 
     // mNotificationManager.notify(NOTIFICATION_ID, notification); 

    } else if (ACTION_DISABLE_STICKING.equals(action)) { 
     stopForeground(true); 
     stopSelf(); 
     // mNotificationManager.cancel(NOTIFICATION_ID); 
    } 
    return super.onStartCommand(intent, flags, startId); 
} 

टिप्पणी की आज्ञाओं यह काम करने के लिए अपने परीक्षणों कर रहे हैं। किसी ने किसी कारण से काम नहीं किया।

मैंने एक नकली गतिविधि भी जोड़ दी क्योंकि यह एक सामग्री चाहता थान्टेंट, लेकिन यह अभी भी काम नहीं करता है।

क्या कोई मदद कर सकता है?

+0

इस पोस्ट में है स्वीकार किए जाते हैं जवाब के साथ अद्यतन किया गया है एक समाधान पर दिनों के लिए काम करने के बाद मेरी समस्या ठीक हो गई। –

उत्तर

9

मुझे थोड़ी देर पहले एक ही समस्या थी, और मुझे पता चला कि किसी कारण से, अधिसूचना आईडी 0 startForeground() के साथ अच्छी तरह से काम नहीं करता है, क्या यह आपके कोड में NOTIFICATION_ID का मूल्य है?


संपादित करें: प्रलेखन अब, राज्य के लिए कि 0, गलत आईडी

+0

हां। यह है। इसे बदलने के बारे में नहीं सोचा था क्योंकि मैं आमतौर पर सबकुछ के लिए इस नंबर से शुरू करता हूं ... यकीन है कि यह इसे ठीक करेगा? –

+0

मुझे जवाब मिला [वहां] (http://stackoverflow.com/questions/8725909/startforeground-does-not-show-my-notification), जाहिर है यह ज्ञात है। यह मेरे लिए और साथ ही उस पोस्ट के ओपी के लिए तय किया गया है, इसलिए इसे आपकी समस्या को ठीक करना चाहिए। जानने का सबसे अच्छा तरीका है :) – Joffrey

+0

हर बार जब मैं खुद से कहता हूं कि मुझे एंड्रॉइड पर कोई भी वीडर बग नहीं मिल रहा है ... –

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