Send a message from an activity that fires an activity that fires when a notification is clicked

I want to send a message from an activity that fires a notification for an activity that fires when the user clicks on the notification.

I just want the EditText value to be sent from one activity to another when I click a button using a notification. I tried to use putExtra on the Intent but then it showed a NullPointer Exception.

Follow this link

How to send parameters from notification notification to action?

I have used onNewIntent but still I am not getting 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>

      

+3


source to share


1 answer


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



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

      

0


source







All Articles