Android Intent.ACTION_CALL, Uri

前端 未结 9 723
南笙
南笙 2020-12-05 16:12

I am trying to use the Intent.Action class. I know how to use the ACTION_VIEW to display a URL but I wanted to use the Intent.ACTION_DIAL to call number when th

9条回答
  •  遥遥无期
    2020-12-05 16:34

    Another approach is to make an PendingIntent to be called later. This is specially util when you want to redirect the user directly to phone call from a notification Action.

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

    You can use it in notification as follow:

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

提交回复
热议问题