另一种方法是使PendingIntent稍后调用。当您想将用户直接从通知操作重定向到电话呼叫时,这是特别有用的。
String number = "551191111113";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" +number));
PendingIntent pendingIntentForCall = PendingIntent.getActivity(mContext, 0 /* Request code */, intent,PendingIntent.FLAG_ONE_SHOT);
您可以在通知中使用它,如下所示:
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setTicker(tickerText)
.setColor(Color.BLACK)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_directions_bus_white_48dp))
.setSmallIcon(R.mipmap.ic_directions_bus_white_24dp)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.addAction(new NotificationCompat.Action(R.mipmap.ic_directions_bus_white_24dp,"Call to " + number,pendingIntentForCall));