共用方式為


Connection.OnShowIncomingCallUi Method

Definition

Notifies this Connection that its ConnectionService is responsible for displaying its incoming call user interface for the Connection.

[Android.Runtime.Register("onShowIncomingCallUi", "()V", "GetOnShowIncomingCallUiHandler", ApiSince=26)]
public virtual void OnShowIncomingCallUi ();
[<Android.Runtime.Register("onShowIncomingCallUi", "()V", "GetOnShowIncomingCallUiHandler", ApiSince=26)>]
abstract member OnShowIncomingCallUi : unit -> unit
override this.OnShowIncomingCallUi : unit -> unit
Attributes

Remarks

Notifies this Connection that its ConnectionService is responsible for displaying its incoming call user interface for the Connection.

Will only be called for incoming calls added via a self-managed ConnectionService (see PhoneAccount#CAPABILITY_SELF_MANAGED), where the ConnectionService should show its own incoming call user interface.

Where there are ongoing calls in other self-managed ConnectionServices, or in a regular ConnectionService, and it is not possible to hold these other calls, the Telecom framework will display its own incoming call user interface to allow the user to choose whether to answer the new incoming call and disconnect other ongoing calls, or to reject the new incoming call.

You should trigger the display of the incoming call user interface for your application by showing a Notification with a full-screen Intent specified.

In your application code, you should create a android.app.NotificationChannel for incoming call notifications from your app:

<code>
            NotificationChannel channel = new NotificationChannel(YOUR_CHANNEL_ID, "Incoming Calls",
                     NotificationManager.IMPORTANCE_MAX);
            // other channel setup stuff goes here.

            // We'll use the default system ringtone for our incoming call notification channel.  You can
            // use your own audio resource here.
            Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            channel.setSound(ringtoneUri, new AudioAttributes.Builder()
                     // Setting the AudioAttributes is important as it identifies the purpose of your
                     // notification sound.
                     .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                     .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                 .build());

            NotificationManager mgr = getSystemService(NotificationManager.class);
            mgr.createNotificationChannel(channel);
</code>

When it comes time to post a notification for your incoming call, ensure it uses your incoming call android.app.NotificationChannel.

<code>
                // Create an intent which triggers your fullscreen incoming call user interface.
                Intent intent = new Intent(Intent.ACTION_MAIN, null);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setClass(context, YourIncomingCallActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_MUTABLE_UNAUDITED);

                // Build the notification as an ongoing high priority item; this ensures it will show as
                // a heads up notification which slides down over top of the current content.
                final Notification.Builder builder = new Notification.Builder(context);
                builder.setOngoing(true);
                builder.setPriority(Notification.PRIORITY_HIGH);

                // Set notification content intent to take user to fullscreen UI if user taps on the
                // notification body.
                builder.setContentIntent(pendingIntent);
                // Set full screen intent to trigger display of the fullscreen UI when the notification
                // manager deems it appropriate.
                builder.setFullScreenIntent(pendingIntent, true);

                // Setup notification content.
                builder.setSmallIcon( yourIconResourceId );
                builder.setContentTitle("Your notification title");
                builder.setContentText("Your notification content.");

                // Set notification as insistent to cause your ringtone to loop.
                Notification notification = builder.build();
                notification.flags |= Notification.FLAG_INSISTENT;

                // Use builder.addAction(..) to add buttons to answer or reject the call.
                NotificationManager notificationManager = mContext.getSystemService(
                    NotificationManager.class);
                notificationManager.notify(YOUR_CHANNEL_ID, YOUR_TAG, YOUR_ID, notification);
</code>

Java documentation for android.telecom.Connection.onShowIncomingCallUi().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to