gpt4 book ai didi

android - 通过通知 Intent 发送 putExtra

转载 作者:行者123 更新时间:2023-11-29 17:36:10 26 4
gpt4 key购买 nike

我想根据代码打开一个特定的选项卡。如果我通过普通按钮执行此操作,它会起作用。

它的工作方式:

public void toSomewhere (View view) {
Intent intent = new Intent(this, SomewhereActivity.class);
intent.putExtra("FirstTab", 2);
startActivity(intent);
}

Intent i = getIntent();
int tabToOpen = i.getIntExtra("FirstTab", -1);
if (tabToOpen!=-1) {
// Open the right tab
}
else {
// Other tab
}

我希望它通过通知工作的方式(此时我有发送通知的代码,但不通过 .putExtra):

public static void NotificationIntent(String title, String message) {
Intent notificationIntent = new Intent(currentContext, SomewhereActivity.class);
**notificationIntent.putExtra("FirstTab", 2);**
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(currentContext, 0, notificationIntent, 0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(currentContext)
.setContentTitle(title)
.setContentIntent(intent)
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
NotificationManager mNotificationManager = (NotificationManager) currentContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}

有谁知道如何解决这个问题,所以它也适用于通知帖子?

最佳答案

使用 FLAG_UPDATE_CURRENT

来自文档:

Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).

This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

代码:

Intent notificationIntent = new Intent(getApplicationContext(), viewmessage.class);
notificationIntent.putExtra("NotificationMessage", notificationMessage);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);

onNewIntent 方法中的 Activity 中:

@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
String notificationMessage = extras.getString("NotificationMessage", "UNDEFINED");
}

还在 onCreate 中转发调用:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

onNewIntent(getIntent());
}

关于android - 通过通知 Intent 发送 putExtra,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30460932/

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