2010-08-21 10 views
7

अधिसूचना बार में कस्टम लेआउट बनाने के तरीके पर पहले से ही कई धागे हैं। समस्या यह है कि मुझे कुछ आसान याद आना चाहिए।अधिसूचना क्षेत्र में प्रगति पट्टी को अद्यतन करना

मैं एक custom_notification_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:padding="3dip" 
       > 

    <TextView android:id="@+id/text" 
       android:text="Uploading" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textColor="#000" 
       /> 

    <ProgressBar 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:max="0" 
      android:progress="0" 
      android:layout_marginLeft="10dip" 
      android:id="@+id/progressBar" 
      /> 
</LinearLayout> 

मैं भी कुछ परीक्षण कोड है कि अधिसूचना, जो काम करता है और प्रगति बार से पता चलता बनाता है है।

NotificationManager mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
Notification notification = new Notification(R.drawable.icon, title, System.currentTimeMillis()); 
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout); 
contentView.setProgressBar(R.id.progressBar, 10, 0, false);   
contentView.setTextViewText(R.id.text, text);  
notification.contentView = contentView; 

Intent notificationIntent = new Intent(context, NotificationHandler.class); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
notification.contentIntent = contentIntent; 
mManager.notify(APPID, notification); 

अंततः मैं प्रगति पट्टी को अद्यतन करने का प्रयास करता हूं, जो काम नहीं करता है।

contentView.setProgressBar(R.id.progressBar, 10, 5, false); 

वास्तव में अधिसूचना को अद्यतन करने का रहस्य क्या है?

+0

संभावित डुप्लिकेट देखें: http://stackoverflow.com/questions/2689729/progress-bar-in-notification-bar –

उत्तर

10

आप इन दो पंक्तियों जोड़ना चाहिए: अपने लेआउट फ़ाइल में

// update the notification object 
notification.contentView.setProgressBar(R.id.progressBar, 10, 5, false); 
// notify the notification manager on the update. 
mManager.notify(APPID, notification); 
+1

इसके साथ समस्या यह है कि यह वास्तव में अधिसूचना को कूदता है (स्थितिानुसार) अधिसूचना क्षेत्र में, यदि वहां है उदाहरण हैं अन्य डाउनलोड चल रहे हैं जो लगातार अपने प्रगति सलाखों को ताज़ा करते हैं ... हालांकि मुझे नहीं पता कि आप इसके बारे में कुछ कर सकते हैं ... – ubuntudroid

+2

आप शायद अपनी अधिसूचना में 'FLAG_ONGOING_EVENT' जोड़ना भूल गए हैं। – Xion

+0

FLAG_ONGOING_EVENT मदद नहीं करता है। अधिसूचना कूदती रहती है। एक एसस ट्रांसफार्मर टैबलेट पर परीक्षण किया। –

1

, आप progressbars 0. पर अधिकतम सेट यह 0 पर maxes है, यह 100 से अधिक 0. यह सेट नहीं जा सकते है

4

अक्सर स्टेटस बार को सूचित न करें याद रखें। यदि आप उस कोड का उपयोग अंदर करते हैं, उदाहरण के लिए, एक AsyncTask पर एक प्रगति अद्यतन और आप प्रत्येक प्रगति को सूचित करते हैं, तो आप वर्चुअल बार को वर्चुअल रूप से अवरुद्ध कर देंगे। परिवर्तन होने पर ही सूचित करें।

0

मुझे प्रगति पट्टी को अक्सर अद्यतन करते समय समस्याएं थीं (मैं नौकरी करने के लिए NotificationCompat.Builder का उपयोग कर रहा हूं) जो अधिसूचना क्षेत्र को अवरुद्ध कर रहा था।

private static final long MIN_UPDATE_INTERVAL = 10000000; 

private long lastUpdateTime = System.nanoTime(); 

और मेरे अद्यतन कॉलबैक में:

// Don't update too often 
if ((System.nanoTime() - lastUpdateTime)< MIN_UPDATE_INTERVAL) return; 

builder.setProgress(max, currentValue, false); 
notificationManager.notify(notificationID, builder.build()); 

lastUpdateTime = System.nanoTime(); 

यह notiicaton क्षेत्र bloking रोकने के लिए और भी एक चिकनी अद्यतन की अनुमति देता है अगर वे इस तरह एक न्यूनतम अंतराल समय के भीतर होती है मैं अद्यतन लंघन द्वारा समस्या हल हो जाती प्रगति पट्टी

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