2010-10-28 28 views
11

मैं अपने ऐप का एक नया उदाहरण शुरू करने के बजाय, अपने ऐप को फिर से शुरू करने के लिए अपनी अधिसूचना प्रोग्राम करने की कोशिश कर रहा हूं ... मैं मूल रूप से वही काम करने की तलाश कर रहा हूं जैसे होम बटन लंबे समय से दबाया जाता है और ऐप वहां से शुरू किया गया है।एंड्रॉइड: अधिसूचना से ऐप को फिर से शुरू कैसे करें?

यहाँ मैं वर्तमान में क्या कर रहा हूँ है:

void notifyme(String string){ 

    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) 
               getSystemService(ns); 

    int icon = R.drawable.notification_icon;  // icon from resources 
    CharSequence tickerText = string + " Program Running...";  // ticker-text 
    long when = System.currentTimeMillis();   // notification time 
    Context context = getApplicationContext();  // application Context 
    CharSequence contentTitle = *********; // expanded message title 
    CharSequence contentText = string + " Program Running...";//expanded msg text 

    Intent notificationIntent = new Intent(this, Main.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(
               this, 0, notificationIntent, 0); 

    // the next two lines initialize the Notification, using the configurations 
    // above 
    Notification notification = new Notification(icon, tickerText, when); 
    notification.setLatestEventInfo(context, contentTitle, contentText, 
                   contentIntent); 
    final int HELLO_ID = 1; 
    mNotificationManager.notify(HELLO_ID, notification); 
} 

मैं अनुमान लगा रहा हूँ कि नई आशय लाइन है, जहां समस्या है ... किसी भी मदद की सराहना की होगी!

+0

संभव डुप्लिकेट में इस विशेषता को दे: http: // stackoverflow.com/questions/5502427/resume-plication-and-stack-from-notification, http://stackoverflow.com/questions/3356095/how-to-bring-android-existing-activity-to-front-via- अधिसूचना – Philipp

+0

इससे मुझे मदद मिली http://stackoverflow.com/questions/3305088/how-to-make-notification-intent-resume-rather -थन-बनाने-एक-नए-इरादे/39482464 # 39482464 – TharakaNirmana

उत्तर

12

आप,

notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

इसके अलावा अपने झंडे निर्धारित करने की आवश्यकता है, तो आप कभी नहीं कभी वहाँ चाहते डुप्लिकेट गतिविधियां की गई यह प्रकट

android:launchMode="singleTask" 
+0

यह केवल तब काम करता है जब ऐप होम बटन के माध्यम से बाहर निकलता है ... लेकिन जब ऐप बैक बटन के माध्यम से बाहर निकलता है तो काम नहीं करता ... कोई विचार कैसे इसे ठीक करो??? –

+0

आपकी अधिसूचना में इंटेंट ने यह संकेत देने के लिए कार्रवाई की है कि आप फिर से शुरू कर रहे हैं, उस क्रिया के लिए जबरदस्त जांच में और तदनुसार व्यवहार करें, किसी भी डेटा को आपको – schwiz

+1

Regent में एक बंडल में ठीक से पुनर्स्थापित करने के लिए आवश्यक है। @ फ्रैंकबोज़ो की टिप्पणी: डिफ़ॉल्ट व्यवहार बैक बटन पर गतिविधि को बंद/नष्ट करना है (यह कम से कम 'adb logcat' मुझे बताता है) - ताकि आप किसी मारे गए गतिविधि को फिर से शुरू नहीं कर सकें ... हालांकि मुझे विश्वास है कि आप कर सकते हैं डिफ़ॉल्ट बैक बटन व्यवहार को ओवरराइड करें (ऐप से बाहर निकलने के लिए 'WebChromeClient' ईवेंट) देखें। – Philzen

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