2010-10-28 14 views
11

Estoy tratando de programar mi notificación para REANUDAR mi aplicación, en lugar de simplemente iniciar una nueva instancia de mi aplicación ... Básicamente estoy buscando que haga lo mismo que cuando se presiona el botón de Inicio y la aplicación se reanuda desde allí.Android: ¿Cómo reanudar una aplicación desde una notificación?

Aquí es lo que estoy haciendo actualmente:

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

que supongo que la nueva línea de Intención es donde radica el problema ... cualquier ayuda se agradece!

+0

posibles duplicados: 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- notificación – Philipp

+0

esto me ayudó http://stackoverflow.com/questions/3305088/how-to-make-notification-intent-resume-rather -than-making-a-new-intent/39482464 # 39482464 – TharakaNirmana

Respuesta

12

debe configurar sus banderas

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

Además, si usted nunca quiere que haya una actividad duplicado que dar a este atributo en el manifiesto

android:launchMode="singleTask" 
+0

Esto solo funciona cuando la aplicación se cierra con el botón de inicio ... pero no funciona cuando la aplicación se cierra con el botón Atrás ... cualquier idea de cómo ¿¿¿arregla esto??? –

+0

en su notificación. Establezca la acción para indicar que está reanudando, en oncreate check para esa acción y compórtese en consecuencia, pase todos los datos que necesite para restaurar correctamente en un paquete en el intento – schwiz

+1

Reg. Comentario de @FrankBozzo: el comportamiento predeterminado es cerrar/destruir la actividad en el botón Atrás (esto es al menos lo que 'adb logcat' me dice) - por lo que no puede reanudar a una actividad muerta ... Sin embargo, creo que puede anular el comportamiento predeterminado del botón de retroceso (consulte los eventos 'WebChromeClient') para no salir de la aplicación. – Philzen

Cuestiones relacionadas