gpt4 book ai didi

android - 从 Android 通知传递数据

转载 作者:行者123 更新时间:2023-11-30 02:22:16 25 4
gpt4 key购买 nike

我已经构建了一个通知并正确显示它,但我不知道如何将数据传递给 Activity 。我从 Intent 中提取了一个字符串作为通知的标题显示,但我需要提取第二个字符串,并让 NotificationHandlerActivity 处理它。

//Intent 服务内部

private void sendNotification(Bundle extras) {
Intent intent=new Intent(this, NotificationHandlerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("link", extras.getString("link"));
mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
long[] vibrate = {100L, 75L, 50L};
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.abc_ic_menu_copy_mtrl_am_alpha)
.setContentTitle(extras.getString("title"))
.setOngoing(false)
.setAutoCancel(true)
.setVibrate(vibrate);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

//NotificationHandlerActivity内部

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
}

最佳答案

您应该在您的 Intent 中使用 extras。因为您的 Intent 目前是匿名的,所以您不能这样做。 Extras 是基本的键值存储。见下文:

public static final String KEY_SECOND_STRING = "keySecondString";
...

...
String secondString = "secondString";
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);

Intent intent = new Intent(this, NotificationHandlerActivity.class);
intent.putExtra(KEY_SECOND_STRING, secondString);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
...

然后,从您的 NotificationHandlerActivity,您可以从 Intent 访问 secondString。

@Override
public void onCreate(Bundle sIS){
super.onCreate();
String secondString = getIntent().getStringExtra("keySecondString");
...
}

关于android - 从 Android 通知传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28313157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com