Java Intent Intents.Insert.ACTION仅在第一次使用时有效?

Java Intent Intents.Insert.ACTION仅在第一次使用时有效?,java,android,android-intent,android-contacts,android-notifications,Java,Android,Android Intent,Android Contacts,Android Notifications,我的Android服务使用自定义按钮创建通知。自定义按钮的操作应打开“创建联系人”系统屏幕,并预先填入电话号码。代码如下: // create the base notification Intent notificationIntent = new Intent(context, MyService.class); PendingIntent pendingIntent = PendingIntent.getService( context, 0, notificationIntent

我的Android服务使用自定义按钮创建通知。自定义按钮的操作应打开“创建联系人”系统屏幕,并预先填入电话号码。代码如下:

// create the base notification
Intent notificationIntent = new Intent(context, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(
    context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle(title)
    .setContentText(text)
    .setAutoCancel(false)
    .setOngoing(true)
    .setContentIntent(pendingIntent);

// add the action to save to contacts
Intent saveIntent = new Intent(ContactsContract.Intents.Insert.ACTION);
saveIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
saveIntent.putExtra(ContactsContract.Intents.Insert.PHONE, text);
saveIntent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE,
    ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
PendingIntent pendingSaveIntent = PendingIntent.getActivity(context, 0, saveIntent, 0);
builder.addAction(0, "Save to Contacts", pendingSaveIntent);

// show the notification
context.getSystemService(Context.NOTIFICATION_SERVICE).notify(0, builder.build());
代码第一次运行良好(假设我将“01234-1234567”传递给它)。但是,后续调用会继续显示第一次显示的相同对话框,其中包含相同的值。因此,即使我在输入中使用不同的数字(Intents.Insert.PHONE额外值的“text”值)再次调用此代码,我也将始终得到“01234-1234567”

我尝试取消“联系人”屏幕,手动创建新联系人,完全停止“联系人”应用程序……不管怎样,下次我点击通知按钮时,我将获得第一次尝试时传递的值的屏幕


你有什么解释吗,或者在代码中看到什么错误吗?

我解决了它。基本上,系统缓存意图,除非它们的不同之处不仅仅是额外的参数。在我的代码中,我只是更改了电话号码,所以它总是使用相同的(第一个)缓存意图。解决方案是使用一个标志来取消当前缓存的意图并创建一个新的意图

PendingIntent pendingSaveIntent = PendingIntent.getActivity(
        context, 0, saveIntent, PendingIntent.FLAG_CANCEL_CURRENT);

系统会兑现您的意图,因此,在PendingEvent中,请使用(PendingEvent.flag_UPDATE_CURRENT),而不是键入0作为标志,尝试以下操作:

getActivity(上下文,0,saveIntent,PendingEvent.FLAG_UPDATE_CURRENT)