gpt4 book ai didi

android - 将消息从启动 Activity 的 Activity 发送到在单击通知时启动的 Activity

转载 作者:太空狗 更新时间:2023-10-29 15:19:14 26 4
gpt4 key购买 nike

我想从启动通知的 Activity 向用户点击通知时启动的 Activity 发送一条消息。

当我通过通知单击按钮时,我只想将一个 EditText 值从一个 Activity 发送到另一个 Activity 。我尝试在 Intent 中使用 putExtra,但随后显示 NullPointer 异常。

转到此链接

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>

最佳答案

通过 API 演示我得到了解决方案进行了以下更改

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

关于android - 将消息从启动 Activity 的 Activity 发送到在单击通知时启动的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9644995/

26 4 0
文章推荐: javascript - 选择文件、设置图像和上传而不阻塞 UI
文章推荐: html - 当父元素可拖动时如何允许文本框插入符定位(通过鼠标单击)
文章推荐: c - 为什么指针转换中需要(void *)?
文章推荐: javascript - 具有嵌入内容的 元素的自动高度