मेरी MainActicity शुरुआत एक अतिरिक्त के RefreshServiceसाथ होती है ।IntentbooleanisNextWeek
मेरे RefreshServiceएक बनाता है Notificationजो मेरे लिए शुरू होता है MainActivityउस पर जब उपयोगकर्ता क्लिक करता है।
यह इस तरह दिखता है:
Log.d("Refresh", "RefreshService got: isNextWeek: " + String.valueOf(isNextWeek));
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra(MainActivity.IS_NEXT_WEEK, isNextWeek);
Log.d("Refresh", "RefreshService put in Intent: isNextWeek: " + String.valueOf(notificationIntent.getBooleanExtra(MainActivity.IS_NEXT_WEEK,false)));
pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
builder = new NotificationCompat.Builder(this).setContentTitle("Title").setContentText("ContentText").setSmallIcon(R.drawable.ic_notification).setContentIntent(pendingIntent);
notification = builder.build();
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_REFRESH, notification);
जैसा कि आप देख सकते हैं कि जिस मूल्य में डाल दिया गया है उसके साथ अतिरिक्त notificationIntentहोना चाहिए ।booleanIS_NEXT_WEEKisNextWeekPendingIntent
जब मैं अभी क्लिक करता हूं तो मुझे Notificationहमेशा इसका falseमूल्य मिलता हैisNextWeek
इस तरह से मुझे इसमें मूल्य मिलता है MainActivity:
isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false);
लॉग इन करें:
08-04 00:19:32.500 13367-13367/de.MayerhoferSimon.Vertretungsplan D/Refresh: MainActivity sent: isNextWeek: true
08-04 00:19:32.510 13367-13573/de.MayerhoferSimon.Vertretungsplan D/Refresh: RefreshService got: isNextWeek: true
08-04 00:19:32.510 13367-13573/de.MayerhoferSimon.Vertretungsplan D/Refresh: RefreshService put in Intent: isNextWeek: true
08-04 00:19:41.990 13367-13367/de.MayerhoferSimon.Vertretungsplan D/Refresh: MainActivity.onCreate got: isNextWeek: false
जब मैं सीधे directlysNextValue` के MainActivityसाथ Intentइस तरह से शुरुआत करता हूं:
Intent i = new Intent(this, MainActivity.class);
i.putExtra(IS_NEXT_WEEK, isNextWeek);
finish();
startActivity(i);
सब कुछ ठीक काम करता है और मुझे trueजब मिलता isNextWeekहै true।
मैं क्या गलत करता हूं कि हमेशा एक falseमूल्य होता है?
अपडेट करें
इससे समस्या हल होती है: https://stackoverflow.com/a/18049676/2180161
उद्धरण:
मेरा संदेह यह है कि चूंकि इंटेंट में बदलती एकमात्र चीज एक्स्ट्रा है, इसलिए
PendingIntent.getActivity(...)फैक्ट्री पद्धति पुराने इरादे को एक अनुकूलन के रूप में फिर से उपयोग कर रही है।ताज़ा करें सेवा में, कोशिश करें:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);देख:
http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT
अद्यतन २
देखें नीचे इस सवाल का जवाब क्यों यह उपयोग करने के लिए बेहतर है PendingIntent.FLAG_UPDATE_CURRENT।