我想从启动通知的活动发送消息到用户单击通知时启动的活动。当我通过通知点击按钮时,我只想发送一个EditText值到另一个活动。我尝试在Intent中使用putExtra,但它显示了NullPointer异常。我查看了这个链接:如何将参数从通知点击发送到活动?。我已经使用了onNewIntent,但仍然没有得到输出。
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>