【问题标题】:Confusing behavior when starting new activity with PendingIntent使用 PendingIntent 开始新活动时的令人困惑的行为
【发布时间】:2010-12-17 16:14:20
【问题描述】:

我正在使用广播监听器来收听传入的电话统计信息,然后推送这样的通知:

    CharSequence contentTitle = "Contact: " + number;
    CharSequence contentText = "Call From: " + number;
    Intent notificationIntent = new Intent(context, CallHandler.class);

    notificationIntent.putExtra(KEYS.NUMBER, number);
    notificationIntent.putExtra(KEYS.STATE, stat);
    notificationIntent.putExtra(KEYS.NAME, name);
    notificationIntent.putExtra(KEYS.DURATION, duration);
    notificationIntent.putExtra(KEYS.START_TIME, startTime);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    notif.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    notif.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify((int) System.currentTimeMillis(), notif);

因此,当有多个呼叫时,我希望有多个呼叫通知,当我单击每个通知时,我希望使用附加参数启动一个活动。但是,我可以看到稍后开始的活动获得了上一个调用的 Intent。

这是我从 Activity 中查看意图的方式

        Bundle extras = getIntent().getExtras();
    String number = extras.getString(KEYS.NUMBER);
    // String state = extras.getString(KEYS.STATE);
    long duration = extras.getLong(KEYS.DURATION);

    Date start = getStartDate();
    ((TextView) findViewById(R.id.subject)).setText("");
    ((TextView) findViewById(R.id.start_time)).setText(tf.format(start));
    ((TextView) findViewById(R.id.end_time)).setText(tf
            .format(getEndDate()));
    ((TextView) findViewById(R.id.caller_number)).setText(number);
    ((TextView) findViewById(R.id.notes)).setText(getNotesDefaultContent(
            number, duration, start));

如何保持 Intent 分别调用独立的活动?

【问题讨论】:

标签: android


【解决方案1】:

试试这个:

PendingIntent.getActivity(context, 0 , notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

注意 PendingIntent.getActitity 方法的最后一个参数。 如果您想知道应该使用哪个 FLAG,请参阅 API 文档 - [http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context, int, android.content.Intent, int)][1]

[1]:http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context, int, android.content.Intent, int)

~多尔夫~

【讨论】:

  • 它的 PendingIntent 不是 PendintIntent
【解决方案2】:

它可以更容易地实现...只需覆盖onNewIntent(Intent intent) 活动类中的方法,只需在其中添加startactivity(intent)

【讨论】:

    【解决方案3】:

    好的,我想我找到了解决方案。诀窍是正确创建 Intent!

    这是我的工作代码:

        CharSequence contentTitle = "Contact: " + number;
        CharSequence contentText = "Call From: " + number;
        Intent notificationIntent = new Intent(context, CallHandler.class);
    
    
        notificationIntent.setAction("com.vantage.vcrm.android.telephony"+System.currentTimeMillis());//unique per contact
    
    
        notificationIntent.putExtra(KEYS.NUMBER, number);
        notificationIntent.putExtra(KEYS.STATE, stat);
        notificationIntent.putExtra(KEYS.NAME, name);
        notificationIntent.putExtra(KEYS.DURATION, duration);
        notificationIntent.putExtra(KEYS.START_TIME, startTime);
        PendingIntent contentIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);
    
        notif.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    
        notif.flags |= Notification.FLAG_AUTO_CANCEL;
    
        notificationManager.notify((int) System.currentTimeMillis(), notif);
    

    你看,我这样做是在我的意图中设置独特的操作:

    notificationIntent.setAction("com.vantage.vcrm.android.telephony"+System.currentTimeMillis());//unique per contact
    

    我还在待处理的 Intent 中发送唯一的请求者 ID:

    PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);
    

    这解决了问题。

    【讨论】:

      猜你喜欢
      • 2019-11-17
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      相关资源
      最近更新 更多