【问题标题】:Android : How to launch Pending intent from another activity with extras?Android:如何从另一个带有附加功能的活动中启动待定意图?
【发布时间】:2012-04-18 04:42:45
【问题描述】:

我面临一些未决意图的问题。我使用通知管理器设置了一些待处理的意图。当用户点击它们时,这些通知会启动一个活动。我在未决意图中使用了 Intent 的一些额外内容。通知点击可以正常工作。

Notification notification = new Notification(icon, tickerText, when);
    Context context = getApplicationContext();

Intent intentOptionDialog = new Intent(Safety_Check_Service.this, Dialog_Safety_Check.class);
    intentOptionDialog.putExtra("startID",startId);
    intentOptionDialog.putExtra("CheckInID", CheckInId);
    intentOptionDialog.putExtra("Frequency", Frequency);
    intentOptionDialog.putExtra("flagFirstShedule", true);

        stopID = (startId + 17);

    intentOptionDialog.putExtra("stopID", stopID);

    PendingIntent contentIntent = PendingIntent.getActivity(Safety_Check_Service.this, DIALOG_ID, intentOptionDialog, 0);

但我的问题是我想从另一个活动中启动这些待处理的意图。创建待处理的意图。如何从另一个活动中启动这些待处理的 Intent,以及如何获取使用待处理 Intent 设置的 Extras?

请帮帮我。

【问题讨论】:

标签: android android-intent android-pendingintent extra


【解决方案1】:

我想你这里有 2 件:

  1. 尝试使用我在Keeping track of sms sent in Android 给出的答案:在创建PendingActivity 时添加FILL_IN_SELECTOR 标志,以强制它携带额外内容。
  2. 获得PendingIntent 后,您只需调用send 方法即可触发它。

【讨论】:

    【解决方案2】:

    您可以在 Intent 中添加数据,然后再使用PendingIntent 将其传递给它

    首先创建一个意图并添加数据

        val pendingIntent = Intent(context, MainActivity::class.java)
        pendingIntent.putExtra(PAGE_KEY, HOME.name)
    

    现在使用标记 PendingIntent.FLAG_UPDATE_CURRENT 从该意图创建待定意图以进行更新。

           PendingIntent.getActivity(
                        context, 0, pendingIntent, PendingIntent.FLAG_UPDATE_CURRENT)
    

    现在在启动的活动 onCreate 方法中像这样访问它

      if (intent != null) {
                val pageValue: String? = intent.getStringExtra(PAGE_KEY)
                pageValue?.let {
                 // do whatever you want
    
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      相关资源
      最近更新 更多