【发布时间】:2016-07-22 22:22:29
【问题描述】:
下面是我在点击通知时用来添加联系人的代码。 除了图片,其他所有内容都会添加到联系人中,我缺少什么?
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream(bmp.getWidth() * bmp.getHeight());
bmp.compress(Bitmap.CompressFormat.PNG, 75, stream);
byte[] byteArray = stream.toByteArray();
final int notificationId = new Random().nextInt();
Intent notificationIntent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
notificationIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
notificationIntent.putExtra(ContactsContract.Intents.Insert.NAME, context.getString(R.string.contact_name));
notificationIntent.putExtra(ContactsContract.Intents.Insert.PHONE, context.getString(R.string.notification_number));
notificationIntent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, context.getString(R.string.notification));
notificationIntent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, context.getString(R.string.monitor_number));
notificationIntent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE_TYPE, context.getString(R.string.monitor_name));
notificationIntent.putExtra(ContactsContract.CommonDataKinds.Photo.PHOTO, byteArray);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle(context.getString(R.string.contact_title))
.setContentText(context.getString(R.string.contact_message))
.setSound(defaultSoundUri)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.contact_message)))
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationId, notification);
【问题讨论】:
-
任何建议都会很有帮助
标签: android-contacts