gpt4 book ai didi

android - 当通过 Intent 传递字符串时,在下一个 Activity 中获取字符串值时,只有一个不为空

转载 作者:行者123 更新时间:2023-11-29 16:08:40 30 4
gpt4 key购买 nike

我将字符串作为 intent extras 传递,以下方法传递的参数包含我希望通过 intent 传递的信息,我可以看到“mc”和“title”中的值与 toasts 中的值一样:

 private void addProximityAlert(int id, Double latitude, Double longitude, String title, String mc) {

Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra("id", id);
intent.putExtra("mc", mc);
intent.putExtra("gl", latitude);
intent.putExtra("title", title);

PendingIntent proximityIntent = PendingIntent.getBroadcast(Map.this, id, intent, 0);
lm.addProximityAlert( latitude, longitude, POINT_RADIUS, PROX_ALERT_EXPIRATION, proximityIntent );

Toast.makeText(getApplicationContext(),"INPAMAP "+intent.getStringExtra("title")+intent.getStringExtra("mc"),Toast.LENGTH_SHORT).show();
}

现在在我的下一节课中,无论我尝试做什么,只有“mc”保留其值,其他变量始终为空,这是下一个 Activity :

@Override
public void onReceive(Context context, Intent intent) {

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent(context, Map.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification notification = createNotification();

//Here I can see title is null and mc is what it should be
Toast.makeText(context,"IN PA "+intent.getStringExtra("title")+intent.getStringExtra("mc") ,Toast.LENGTH_SHORT).show();

我尝试过使用 bundle ,但没有用,我完全不知道为什么只有 mc 被传递,而 mc 和 title 都是字符串。我试过只传递 mc 或 title,但仍然只有 mc 不为空。我试图更改它们传递给 addproximityAlert 方法的顺序以及它们作为附加项添加的顺序,我没有选择。我实际上想知道如何在 Eclipse 中正确调试,这样我就可以在来到 SO 之前自己解决这些问题,但是我面临很多时间压力。

非常感谢您提供有关如何找出我的问题所在的建议,谢谢。

最佳答案

您需要在 PendingIntent.getXXX 方法中设置 PendingIntent.FLAG_UPDATE_CURRENT 标志。

PendingIntent.getBroadcast(Map.this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

问题是 PendingIntent.getXXX 缓存了 intents 并且可以返回以前创建的实例而不更新它。 FLAG_UPDATE_CURRENT 强制它更新 Intent 。您可以在 official documentation 中找到更详细的解释。 .

关于android - 当通过 Intent 传递字符串时,在下一个 Activity 中获取字符串值时,只有一个不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15462002/

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