Envíe el mensaje de la actividad que inicia la actividad a la que se inicia al hacer clic en la notificación

I want to send a message from the activity which is launching the notification to the activity which gets launched when a user clicks on the notification.

Solo quiero un Editar texto value to be sent from one activity to another when I click the button via the notification. I tried to use putExtra in the Intent but then it showed NullPointer Exception.

Went to this link

¿Cómo enviar parámetros desde una notificación-clic a una actividad?

I have used onNewIntent but still I don't get the output

package com.pdd.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class NotificationDemoActivity 
extends Activity implements OnClickListener
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button b=(Button) findViewById(R.id.button1);
    b.setOnClickListener(this);
}

public void onClick(View v) {

    EditText et=(EditText) findViewById(R.id.editText1);
    String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = 
                       (NotificationManager)getSystemService(ns);

    int icon = android.R.drawable.ic_dialog_alert;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);


    Context context = getApplicationContext();
    CharSequence contentTitle = "My notification";
    CharSequence contentText = "Hello World!";
    Intent notificationIntent = new Intent(this, Second.class);
    notificationIntent.putExtra("im",et.getText().toString() );
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,    
    notificationIntent, 0);


    notification.setLatestEventInfo(context, contentTitle, contentText, 
    contentIntent);

    final int HELLO_ID = 1;

    mNotificationManager.notify(HELLO_ID, notification);

}
}

package com.pdd.notification;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;


public class Second extends Activity  {


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.second);

    /*Intent i=getIntent();
    if(i==null)
        return;

    Bundle b=i.getExtras();

    String s=b.getString("im"); //incoming message

    EditText et=(EditText) findViewById(R.id.editText1);

    et.setText(s);


    */
}

@Override
protected void onNewIntent(Intent intent) {

    //Intent i=getIntent();

    if(intent!=null)
    {


    Bundle b=intent.getExtras();

    String s=b.getString("im"); //incoming message

    EditText et=(EditText) findViewById(R.id.editText2);

    et.setText(s);
    Log.d("p",s);
    }
    super.onNewIntent(intent);
}
 }

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pdd.notification"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".NotificationDemoActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <activity android:name=".Second" android:launchMode="singleTop"></activity>
   </application>

 </manifest>

preguntado el 10 de marzo de 12 a las 08:03

+1 for best question title in history -

1 Respuestas

I got the solution after going through API demos Made the following change

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

respondido 10 mar '12, 21:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.