6

पर प्रगति अधिसूचना पंक्ति प्रदान करना मैं अधिसूचना बार में फ़ाइलों के एकाधिक डाउनलोड दिखाने में सक्षम होना चाहता हूं जिसे रद्द भी किया जा सकता है।कस्टम एंड्रॉइड डाउनलोड सेवा - प्रति फ़ाइल

मैंने एक कस्टम सेवा लागू की है जो AsyncTasks का उपयोग करके समानांतर में एकाधिक डाउनलोड करता है। OnPublishProgress मैं प्रत्येक फ़ाइल के लिए डाउनलोड प्रगति दिखाने के लिए अधिसूचना बार में अलग-अलग पंक्तियों को अद्यतन करने का प्रयास कर रहा हूं। दो ठोस दिनों के लिए मैं पंक्तियों के झटके, ऑर्डर स्वैपिंग और कभी-कभी केवल रिक्त होने या केवल एक पंक्ति को अपडेट करने के साथ समस्याओं को ठीक करने का प्रयास कर रहा हूं। साथ ही, नियमित रूप से रद्द करने के लिए पंक्ति को टैप करना हमेशा काम नहीं करता है।

protected void showProgressNotification(final File item, int progress, boolean isDownloading) { 
    String message = null; 
    int smallIcon = 0; 
    Bitmap largeIcon = null; 
    int flags = 0; 
    flags |= Notification.FLAG_ONGOING_EVENT; 
    //flags |= Notification.FLAG_FOREGROUND_SERVICE; 
    //flags |= Notification.FLAG_ONLY_ALERT_ONCE; 
    //flags |= Notification.FLAG_AUTO_CANCEL; 

    NotificationCompat.Builder builder = 
      new NotificationCompat.Builder(getApplicationContext()); 
    builder.setAutoCancel(true); 

    if (progress == 100) { 
     largeIcon = BitmapFactory.decodeResource(getResources(), 
       O2FolderListAdapter.getIconForItem(item, false)); 
     smallIcon = R.drawable.ic_cloud_upto_date; 

     if (isDownloading) { 
      message = "Download completed. Tap to clear."; 
     } else { 
      message = "Upload completed. Tap to clear."; 
     } 
    } else if (progress >= 0) { 
     largeIcon = BitmapFactory.decodeResource(getResources(), 
       O2FolderListAdapter.getIconForItem(item, true)); 
     if (isDownloading) { 
      smallIcon = R.drawable.ic_cloud_downloading; 
      message = "Downloading: " + progress + "%. Tap to cancel."; 
     } else { 
      smallIcon = R.drawable.ic_cloud_uploading; 
      message = "Uploading: " + progress + "%. Tap to cancel."; 
     } 
     builder.setProgress(100, progress, false); 
    } else { 
     largeIcon = BitmapFactory.decodeResource(getResources(), 
       O2FolderListAdapter.getIconForItem(item, true)); 
     smallIcon = R.drawable.ic_cloud_conflict; 
     if (isDownloading) 
      message = "Cancelled download. Tap to clear."; 
     else 
      message = "Cancelled upload. Tap to clear."; 
    } 

    if (mResultIntent == null) { 
     mResultIntent = new Intent(getApplicationContext(), CustomDownloadService.class); 
     mResultIntent.addFlags(Notification.FLAG_ONGOING_EVENT); 
    } 
    mResultIntent.putExtra("cancel", item.getPath().hashCode()); 
    Log.d("O2AbstractDownloadService", "Setup task id " + item.GetPath().hashCode()); 
    if (mContentIntent == null) 
     mContentIntent = PendingIntent.getService(getApplicationContext(), PI_REQ_CODE, mResultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.setContentIntent(mContentIntent); 

    builder.setLargeIcon(largeIcon); 
    builder.setSmallIcon(smallIcon); 
    builder.setContentTitle(item.GetName()); 
    builder.setContentText(message); 

    //if (progress != 100) 
     //builder.addAction(R.drawable.ic_action_dark_cancel, "Cancel", contentIntent); 

    final Notification notification = builder.build(); 
    notification.flags = flags; 
    notification.defaults = Notification.DEFAULT_LIGHTS; 

    NotificationManager mNotificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    // Id allows you to update the notification later on. 
    //mNotificationManager.notify(item.getPath().hashCode(), notification); 
    //startForeground(item.getPath().hashCode(), notification); 

    // only update notification every 100ms (unless cancel or complete) 
    long notificationDelay = 100; 
    long now = System.currentTimeMillis(); 
    if (mFutureCallTime == 0 || now > mFutureCallTime || progress == -1 || progress == 100) { 
     startForeground(item.getPath().hashCode(), notification); 
      //mNotificationManager.notify(item.GetPath().hashCode(), notification); 
    } else 
     Log.d("CustomDownloadService", "Called too often to notification"); 

    mFutureCallTime = now + notificationDelay; 
} 

तो मैं जब अधिसूचना पर दोहन, फ़ाइल का आईडी गुजर डाउनलोड को रद्द करना सेवा कॉल करने के लिए सेटअप करने के लिए कोशिश कर रहा हूँ कार्रवाई:

यहाँ मेरी कोड है। क्या कोई देख सकता है कि मैं क्या गलत कर रहा हूं? क्या मैं वास्तव में संभव के बाद क्या कर रहा हूँ? ज़ूम टैबलेट पर अधिसूचनाएं बहुत अधिक होती हैं, लेकिन नेक्सस 7 पर इतनी बार नहीं होती है। सभी डिवाइस लगातार चलने वाली पंक्तियों को बदलते हैं जिसका मतलब है कि आप जो डाउनलोड चाहते हैं उसे रद्द करना लगभग असंभव है।

किसी भी सलाह की सराहना की जाएगी।

अद्यतन 1: मुझे लगता है कि यह मेरी मुद्दों में से एक कारण हो सकता है: Android Service.startForeground does NOT respect notification id uniqueness

अद्यतन 2: बाहर गमागमन मुद्दा builder.setWhen (fixedTime) पर कॉल करके तय हुई थी। जाहिर है, नई तारीख समय पंक्तियों को रीफ्रेश किए जाने पर प्रत्येक बार फिर से व्यवस्थित कर रहा था। बस ज़ूम पर झिलमिलाहट और 'रद्द करने के लिए टैप करें' सुविधा को ठीक करने की आवश्यकता है।

अद्यतन 3: ज़ूम पर झटके को ताज़ा करने के लिए कॉल सीमित करने के साथ तय किया गया था। अंत में कोड अधिसूचना को 100ms से अधिक बार अपडेट होने से रोकता है। शेष मुद्दों को रद्द करने के साथ करना है। रद्द करने के लिए टैप पहली बार काम करता है लेकिन बाद की फ़ाइलों पर काम नहीं करता है। इसके अलावा मैं पंक्तियों को साफ़ नहीं कर सकता।

अद्यतन 4: ही मुद्दे को रद्द resultIntent क्षेत्र श्रेणी स्तर पर होने से कारण था। जब मैंने प्रत्येक बार एक नया निर्माण किया, तो मैंने अधिसूचना को ताज़ा कर दिया, आईडी ने बांध लिया। मैंने ध्वज को अधिसूचना में बदल दिया। FLAG_ONLY_ALERT_ONCE केवल और केवल उपयोग किया गया। नोटिफ़ाई() और प्रारंभ नहीं करना चाहिए()।

+0

तो सभी मुद्दे अभी तय किए गए हैं? यदि ऐसा है, तो कृपया अपना उत्तर पोस्ट करें और इसे भविष्य के संदर्भ के लिए स्वीकृत के रूप में चिह्नित करें! Thnx – Entreco

उत्तर

1

सभी मुद्दों को ठीक किया गया था। मैंने अपनी मूल पोस्ट में अपडेट जोड़े हैं। सारांश में: 1) builder.setWhen (निश्चित समय) के साथ सावधान रहें। 2) प्रत्येक 100ms 3 से अधिक बार ताज़ा न करें 3) सही झंडे सेट करें।

+1

ताज़ा करने के लिए किसी भी अधिसूचना को याद कर सकते हैं। यहां कुछ विधियां दी गई हैं जिन्हें कभी-कभी अद्यतन करने के लिए उपयोग किया जा सकता है: http://stackoverflow.com/questions/16050381/how-do-i-get-an-android-service-to-broadcast-an-intent-every-few- सेकंड –

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