2013-01-15 16 views
5

लौटाता है मैं एक अधिसूचना और घटना onCreate के माध्यम से एक गतिविधि चलाने की कोशिश कर रहा हूं, मैं "रीडायरेक्ट" करना चाहता हूं। इसके लिए Intent कक्षा में जानकारी पर एक विचार जोड़ें। एक महत्वपूर्ण विशेषता यह है कि अधिसूचना उत्पन्न करने वाली कक्षा एक सेवा के माध्यम से की जाती है। मैं android.app.Application वर्ग द्वारा प्रदान की गई getApplicationContext विधि से संदर्भ पुनर्प्राप्त करता हूं। जब भी मैं विधि getExtras() कॉल करता हूं null लौटा रहा है। मैं क्या गलत कर रहा हूं?Intent.getExtras() हमेशा शून्य

public class OXAppUpdateHandler { 

    private void addNotification(Context context, int iconID, 
      CharSequence tickerText, CharSequence title, CharSequence content) { 

     CharSequence notificationTicket = tickerText; 
     CharSequence notificationTitle = title; 
     CharSequence notificationContent = content; 

     long when = System.currentTimeMillis(); 

     Intent intent = new Intent(context, MainActivity_.class); 
     intent.setFlags(
      Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     intent.putExtra(OPEN_UPDATE_ACTIVITY_KEY, 1); 

     PendingIntent pendingIntent = 
      PendingIntent.getActivity(context, 0, intent, 0); 

     NotificationManager notificationManager = 
      (NotificationManager) context.getSystemService(
       Context.NOTIFICATION_SERVICE); 
     Notification notification = 
      new Notification(iconID, notificationTicket, when); 
     notification.setLatestEventInfo(context, notificationTitle, 
             notificationContent, pendingIntent); 
     notificationManager.notify(NOTIFICATION_ID, notification); 
    } 

    public static boolean isUpdateStart(Intent intent) { 
     Bundle bundle = intent.getExtras(); 
     boolean result = bundle != null && 
         bundle.containsKey(OPEN_UPDATE_ACTIVITY_KEY); 
     if (result) { 
      bundle.remove(OPEN_UPDATE_ACTIVITY_KEY); 
     } 
     return result; 
     } 
    } 

    @EActivity(R.layout.activity_main) 
    public class MainActivity extends Activity { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      if (OXAppUpdateHandler.isUpdateStart(getIntent())) { 
       startActivity(new Intent(this, UpdateActivity_.class)); 
      } 
     } 
    } 
+0

क्या आप कोड पोस्ट कर सकते हैं जो अतिरिक्त इरादे रखता है? –

+0

बेस कॉन्टेक्स्ट प्राप्त करने का प्रयास करें, मुझे अधिसूचना प्रसारण या कुछ और कहां मिल रहा है। –

+0

@ChangdeoJadhav विधि OXAppUpdateHandler। कोड intent.putExtra (OPEN_UPDATE_ACTIVITY_KEY, 1) के माध्यम से addNotification (...); – adrianosepe

उत्तर

20

मैं खिड़की से बाहर दुबला करने जा रहा हूँ और लगता है कि आपकी समस्या यहाँ है:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
इस प्रकार आप एक बूलियन मान के लिए जाँच रहे हैं, तो आप जो डेटा बंडल में जगह मिलना चाहिए

आप intent से getActivity() पास कर रहे हैं और उम्मीद कर रहे हैं कि आप PendingIntent वापस प्राप्त करेंगे जो आपके Intent से मेल खाता है और इसमें आपके अतिरिक्त शामिल हैं। दुर्भाग्य से, अगर वहाँ पहले से ही एक PendingIntent प्रणाली है कि ( को ध्यान में लेने के अपने Intent एक्स्ट्रा कलाकार के बिना ) अपने Intent मैचों में चारों ओर चल रहा है तो getActivity() कि PendingIntent बजाय आप वापस आ जाएगी। यदि यह समस्या है

देखने के लिए, इस प्रयास करें:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 
        PendingIntent.FLAG_UPDATE_CURRENT); 

यह कहा गया है कि अगर वहाँ पहले से ही एक PendingIntent कि आपके Intent प्रणाली है कि उस में लोगों के साथ एक्स्ट्रा कलाकार बदलना चाहिए में कहीं से मेल खाता है आपके intent पैरामीटर।

+0

लौट रहा है यह मेरे लिए काम करता है! मैं इसलिए भी कि गतिविधि हमेशा निर्मित नहीं किया गया था नीचे कोड में शामिल हैं: '@EActivity (R.layout.activity_main) सार्वजनिक वर्ग MainActivity फैली OXBaseActivity { \t ... \t @Override \t संरक्षित शून्य onNewIntent (इरादा इरादा) \t { \t \t सुपर।onNewIntent (आशय); \t \t \t \t अगर (OXAppUpdateHandler.isUpdateStart (आशय)) \t \t { \t \t \t startActivity (नई आशय (यह, UpdateActivity_.class)); \t \t} \t} } ' – adrianosepe

+1

गतिविधि हमेशा से निर्मित नहीं है क्योंकि आप दोनों' Intent.FLAG_ACTIVITY_CLEAR_TOP' और 'Intent.FLAG_ACTIVITY_SINGLE_TOP' निर्धारित किया है। यदि आप गतिविधि को हमेशा पुनर्निर्मित करना चाहते हैं, तो 'प्राप्त करें' से 'Intent.FLAG_ACTIVITY_SINGLE_TOP' को हटाएं जिसे आप 'getActivity()' पर भेजते हैं। –

+0

यह काम किया। मैं लंबित INTent.FLAG_UPDATE_CURRENT के बजाय 0 पास कर रहा था। बहुत धन्यवाद। –

-1

(1) here की जाँच करें आप पुट उपयोग कर रहे हैं यह सुनिश्चित कर लें/एक्स्ट्रा कलाकार सही ढंग से मिल के रूप में मैं कोड जहां आप डेटा आशय जोड़ रहे हैं नहीं देख पा रहे है।

(2) ऐसा लगता है कि आप इरादे को कॉल नहीं कर रहे हैं और अतिरिक्त प्राप्त कर रहे हैं और नतीजतन, वास्तव में बंडल से कुछ भी नहीं मिल रहा है (मानते हुए कि जानकारी विस्तारित है)।

Bundle bundle = getIntent().getExtras(); 

if (bundle.getBooleanExtra("WHATEVER"){ 
    //whatever you want to do in here 
} 
+0

मैं '... intent.putExtra (OPEN_UPDATE_ACTIVITY_KEY, 1) में जानकारी जोड़ने,()' और उसके बाद ठीक हो 'बंडल बंडल = intent.getExtras; बूलियन परिणाम = बंडल! = null && bundle.containsKey (OPEN_UPDATE_ACTIVITY_KEY); ' – adrianosepe

+0

लेकिन आपको अतिरिक्त सही तरीके से नहीं मिल रहा है। । । क्या आपने सुझाए गए तरीके की कोशिश की? – Rarw

+0

मेरे मामले में जब मैं getIntent() को कॉल करता हूं। GetExtras() शून्य – adrianosepe

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