简体   繁体   English

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

[英]Intent Intents.Insert.ACTION only works the first time?

My Android service creates a notification with a custom button. 我的Android服务使用自定义按钮创建通知。 The action for the custom button should open the "Create contact" system screen, with the phone number prefilled. 自定义按钮的操作应打开“创建联系人”系统屏幕,并预先填写电话号码。 This is the code: 这是代码:

// 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());

The code works well the first time (let's say that I pass "01234-1234567" to it). 该代码在第一次运行时效果很好(假设我将“ 01234-1234567”传递给了它)。 However, subsequent calls keep showing the same dialog shown the first time, with the same value in it. 但是,随后的调用将继续显示第一次显示的对话框,并且对话框中具有相同的值。 So even if I call this piece code again with a different number in input (the value of "text" for the "Intents.Insert.PHONE" extra value) I'll always get "01234-1234567". 因此,即使我再次使用输入中的另一个数字(“ Intents.Insert.PHONE”额外值的“ text”值)再次调用该邮政编码,我也始终会得到“ 01234-1234567”。

I tried cancelling the Contacts screen, manually create a new contact, completely stopping the Contacts apps...no matter what, next time I tap on the notification's button I'll get the screen with the value I originally passed on the first attempt. 我尝试取消了“联系人”屏幕,手动创建了一个新联系人,完全停止了“联系人”应用程序……无论如何,下次我点击通知按钮时,屏幕上将显示我第一次尝试时传递的值。

Do you have any explanation or see anything wrong in the code? 您在代码中有任何解释或发现任何错误吗?

I solved it. 我解决了 Basically, the system caches intents, unless they differ for something more than just the extra params. 基本上,系统会缓存意图,除非它们的区别不仅仅是多余的参数。 In my code I was only changing the phone number, so it was always using the same (first) cached intent. 在我的代码中,我仅更改电话号码,因此它始终使用相同的(第一个)缓存意图。 The solution is to use a flag to cancel the currently cached intent and create a new one 解决方案是使用标志来取消当前缓存的意图并创建一个新的意图

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM