2013-09-01 14 views
8

Ho riscontrato problemi Il mio MainActivity può essere creato in 3 modi: 1) Avvio standart App 2) dal servizio 3) dal clic di notifica. Come posso verificare quando inizia dalla notifica clic?Come verificare se Attività inizia dalla notifica

codice di notifica:

private void createNotification() 
{ 
    Log.d("service createNotification",MainActivity.TAG); 
    Context context = getApplicationContext(); 
    Intent notificationIntent = new Intent(this,MainActivity.class); 
    intent.putExtra(AppNames.IS_NOTIFICATION_INTENT,true); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
      notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
      .setContentTitle(this.getString(R.string.notification_title)) 
      .setContentText(this.getString(R.string.notification_text))    
      .setContentIntent(pendingIntent)    
      .setSmallIcon(R.drawable.ic_launcher); 

    getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(AppNames.APP_NOTIFICATION, builder.getNotification()); 
} 

risposta

8

aggiungere

intent.putExtra("started_from","notification"); 

al codice che inizia l'intento dalle notifiche, e la stessa cosa per l'altra startActivity chiamate in appena modificare il valore, quindi all'interno della vostra attività

String startedFrom = getIntent().getStringExtra("started_from"); 

per ulteriori rimandi a questa domanda: How do I get extra data from intent on Android?

+2

unfochnately questo metodo non funziona, riceve sempre notifica in tutti e 3 i casi. – Abbath

+0

è necessario modificare "notifica" in putExtra per qualcos'altro, per "servizio" per esempio. –

+0

non funziona affatto, guarda @http: //stackoverflow.com/questions/14122085/determine-if-activity-is-called-by-a-notification if any1 interested –

Problemi correlati