2010-07-28 14 views
13

के माध्यम से एंड्रॉइड मौजूदा गतिविधि को सामने कैसे लाया जाए मेरे पास एक एंड्रॉइड एप्लिकेशन है, जब यह सेवा चलाता है, तो मैं स्टेटस बार पर अधिसूचना दिखाना चाहता हूं। फिर उपयोगकर्ता होम कुंजी दबाकर अन्य एप्लिकेशन पर नेविगेट कर सकता था। हालांकि जब मैं अधिसूचना आइकन के माध्यम से पिछले चल रहे एप्लिकेशन को सामने लाने की कोशिश करता हूं, तो मौजूदा गतिविधि में कुछ समस्या है। यहां तक ​​कि मैं इसे "सिंगल टॉप" मोड के रूप में घोषित करता हूं (मैं मौजूदा गतिविधि को चलाने के लिए चाहता हूं क्योंकि एक संबंधित सेवा चल रही है), किसी भी तरह गतिविधि पर ऑनडेस्ट्रॉय को ऑनर्यूम से पहले बुलाया गया है। अधिसूचना वस्तु बनाने का मेरा कोड यहां दिया गया है। क्या आप कृपया मुझे बता सकते हैं कि यह क्या गलत है। धन्यवाद।अधिसूचना

private void showNotification() 
{ 

    Intent toLaunch = new Intent(getApplicationContext(), 
              MySingleTopActivity.class); 

    PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT); 

    notification.setLatestEventInfo(getApplicationContext(), 
     getText(R.string.GPS_service_name), text, intentBack); 
.... 
} 
+0

संभावित डुप्लिकेट: http://stackoverflow.com/questions/4047683/android-how-to-resume-an-app-from-a-notification, http://stackoverflow.com/questions/5502427/resume-application-and-stack-from-अधिसूचना – Philipp

उत्तर

-2

जब कोई गतिविधि पृष्ठभूमि में होती है, तो एंड्रॉइड किसी भी समय संसाधनों को मुक्त करने के लिए गतिविधि को मार सकता है। जीवन चक्र पर पूर्ण विवरण के लिए Activity दस्तावेज़ीकरण देखें।

आपकी गतिविधि को ऑन-ऑन या ऑनवेस्टेंसस्टेट विधियों में अपने राज्य को सहेजने के लिए तैयार होने की आवश्यकता है। उपरोक्त संदर्भित दस्तावेज़ में लगातार राज्य को बचाने पर अतिरिक्त विवरण हैं।

+1

मुझे नहीं लगता कि गतिविधि "onDestroy" द्वारा बुलाया जाना चाहिए। जब पृष्ठभूमि पर जाता है तो केवल "ऑन पॉज़" कहा जाता है। और "ऑनस्यूम" को कॉल किया जाता है जब यह फ्रंटेंड पर वापस आ जाता है। यदि मैं एप्लिकेशन को फिर से शुरू करने के लिए बस एप्लिकेशन आइकन पर क्लिक करता हूं, तो कोई समस्या नहीं है, केवल "ऑनर्यूसम" कहा जाता है। हालांकि अगर मैं अधिसूचना आइकन पर क्लिक करने का प्रयास करता हूं, तो गतिविधि "ऑनडेस्ट्राय" -> "ऑनर्यूसम" जाती है, जो समस्या का कारण बनती है। – user404012

+0

ऑन पॉज़ को पृष्ठभूमि में जाने पर बुलाया जाता है। हालांकि, संदर्भित दस्तावेज़ में इसके राज्यों के रूप में, एंड्रॉइड किसी भी समय गतिविधि को मारने का विकल्प चुन सकता है यदि उसे संसाधनों की आवश्यकता होती है, और इस प्रकार डीस्ट्रॉय को बुलाया जाएगा। आप इस तथ्य पर निर्भर नहीं हो सकते कि ऑनस्ट्राय को नहीं बुलाया जाएगा, इस प्रकार आपको राज्य को बचाकर इसे ठीक से निपटने की जरूरत है। –

23
private void showNotification() { 
    Intent toLaunch = new Intent(getApplicationContext(), MySingleTopActivity.class); 

    //add these two lines will solve your issue 
    toLaunch.setAction("android.intent.action.MAIN"); 
    toLaunch.addCategory("android.intent.category.LAUNCHER"); 

    PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0, toLaunch, PendingIntent.FLAG_UPDATE_CURRENT); 

    notification.setLatestEventInfo(getApplicationContext(), getText(R.string.GPS_service_name), text, intentBack); 
    ... 
} 
+0

अधिसूचना.सेट LatestEventInfo (getAplicationContext(), getText (R.string.GPS_service_name), टेक्स्ट, इरादाबैक); टेक्स्ट को लॉन्च करना चाहिए मुझे लगता है .. – Siddhesh

+0

+1 उल्लेख के साथ कि लंबित इंटेंटेंट घोषणा 'लंबित सामग्री सामग्री' होना चाहिए = लंबित INTent.getActivity (यह, 0, tolunch, 0); ' – Calin

8

मैं, आप यह कर की सलाह देते हैं

private void showNotification() 
{ 

Intent toLaunch = new Intent(getApplicationContext(), DummyActivity.class); 

// You'd need this line only if you had shown the notification from a Service 
toLaunch.setAction("android.intent.action.MAIN"); 

PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT); 

.... 
} 

DummyActivity बस एक गतिविधि कि हमेशा OnCreate स्थिति में खुद को खत्म किया जाना चाहिए।

मैनिफ़ेस्ट फ़ाइल में, जोड़ने के इन पंक्तियों

<activity class=".DummyActivity"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
    </intent-filter> 
</activity> 

मुझे आशा है कि इस मदद करता है ...

+0

यह एकमात्र समाधान था जो मेरे लिए काम करता था एंड्रॉइड 2.2+। धन्यवाद – Darcy

+0

खुशी है कि यह आपके लिए काम करता है :) –

+0

@ शार्क अब्दुल्ला यह एक बहुत ही चालाक कामकाज है, धन्यवाद :) – Lev

0

अपने पैकेज के इरादे शुरू करने के लिए एक और तरीका है।

private void NotificationwithLaucherSelfPackage(Context context , int notification_id){ 
     Notification noti = new Notification.Builder(context) 
       .setContentTitle("Your Title") 
       .setContentText("Your Text") 
       .setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha) 
       .setContentIntent(PendingIntent.getActivity(context ,notification_id , getLauncherIntent(context) , PendingIntent.FLAG_UPDATE_CURRENT)) 
       .build(); 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
    } 

private Intent getLauncherIntent(Context context){ 
     return context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); 
    } 
संबंधित मुद्दे