我想从活动中发送一条消息,该消息会向用户点击通知时启动的活动启动通知。
当我通过通知单击按钮时,我只想将 EditText 值从一个活动发送到另一个活动。 我试图在Intent中使用putExtra但是它显示出NullPointer Exception。
去了这个链接
How to send parameters from a notification-click to an activity?
我使用过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>
答案 0 :(得分:0)
我在通过API演示后得到了解决方案 做了以下改变
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);