2010-10-28 10 views
11

Ich versuche meine Benachrichtigung zu RESUME meiner App zu programmieren, anstatt einfach eine neue Instanz meiner App zu starten ... Ich bin im Grunde darauf aus, um dasselbe zu tun, wenn die Home-Taste lang gedrückt ist und die App wird von dort wieder aufgenommen.Android: Wie man eine App von einer Benachrichtigung wieder aufnimmt?

Hier ist, was ich derzeit tue:

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); 
} 

Ich vermute, dass die neue Intent Linie ist, wo das Problem liegt ... jede mögliche Hilfe würde geschätzt!

+0

möglich Duplikate: http: // stackoverflow.com/questions/5502427/resume-application-and-stack-from-notification, http://stackoverflow.com/questions/3356095/how-to-bring-android-existing-activity-to-front-via- Benachrichtigung – Philipp

+0

das hat mir geholfen http://stackoverflow.com/questions/3305088/how-to-make-notification-intent-resume-rather -than-make-a-new-intent/39482464 # 39482464 – TharakaNirmana

Antwort

12

Sie benötigen, um Ihre Flaggen

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

auch einstellen, wenn Sie überhaupt nie da wollen eine doppelte Aktivität zu geben dieses Attribut in den manifesten

android:launchMode="singleTask" 
+0

Das funktioniert nur, wenn die App über die Home-Taste beendet wird ... funktioniert aber nicht, wenn die App über den Zurück-Button verlassen wird ... keine Ahnung, wie repariere das??? –

+0

in Ihrem notificationIntent setzen Sie die Aktion, um anzugeben, dass Sie wieder aufnehmen, in onCreate überprüfen Sie für diese Aktion und verhalten sich entsprechend, übergeben Sie alle Daten, die Sie ordnungsgemäß in einem Bundle in der Absicht wiederherstellen müssen – schwiz

+1

Reg. @FrankBozzo 's Kommentar: Standardverhalten ist das Schließen/Zerstören der Aktivität auf der Zurück - Taste (das ist zumindest, was' adb logcat 'sagt mir) - so können Sie nicht zu einer getöteten Aktivität fortsetzen ... Aber ich glaube, dass Sie können Überschreiben Sie das Verhalten der Standard-Zurück-Schaltfläche (siehe WebChromeClient-Ereignisse), um die Anwendung nicht zu beenden. – Philzen

Verwandte Themen