如何使用待定意图启动新的活动

21

请问如何使用 PendingIntent 启动一个新的 Activity 并通过 pending intent 传递一个值。 提前感谢。

1个回答

33
Intent intent = new Intent(getApplicationContext(), ActivityToLaunch.class);
intent.putExtra(<oneOfThePutExtraFunctions>);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

通过使用 Intent.PutExtra() 函数之一,可以向 Intent 添加额外的数据。这些函数位于以下链接中:http://developer.android.com/reference/android/content/Intent.html

然后,在准备启动您的 PendingIntent 时,使用其中一个 Send() 函数。这些函数位于以下链接中:http://developer.android.com/reference/android/app/PendingIntent.html


3
PendingIntent.getActivity 的文档中提到:“请注意,该活动将在现有活动的上下文之外启动,因此您必须在 Intent 中使用 Intent.FLAG_ACTIVITY_NEW_TASK 启动标志。” 因此,您可能应该执行 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - user905686
1
从31版本开始,您需要像这样指定PendingIntent.FLAG_IMMUTABLE:PendingIntent pi = PendingIntent.getActivity(requireActivity(), 0, intent, PendingIntent.FLAG_IMMUTABLE); - Smeet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接