Android的ConnectionService在onShowIncomingCallUi时未显示来电界面。

huangapple 未分类评论53阅读模式
英文:

Android.ConnectionService Incoming call UI not showing onShowIncomingCallUi

问题

我正在使用TelecomManagerConnectionService和Connection构建基本的呼叫应用程序但是当有一个来电时我的incomingActivity界面没有显示出来以下是目前的示例代码

在我的MainActivity.java中

```java
Intent intent = new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER);
intent.putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, getPackageName());

startActivity(intent);

// ================================================================
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    TelecomManager manager = (TelecomManager) getSystemService(TELECOM_SERVICE);

    PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
            new ComponentName(getApplicationContext(), CallHandler.TAG), "myCallHandlerId");

    PhoneAccount phoneAccount = PhoneAccount
                            .builder(phoneAccountHandle, "myCallHandlerId")
                            .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)
                            .build();

    manager.registerPhoneAccount(phoneAccount);

    // ... 其他操作 ...

    if(manager.isIncomingCallPermitted(phoneAccountHandle)){
        manager.addNewIncomingCall(phoneAccountHandle, bundle);
    }
}

在我的CallHandler.java中:

@RequiresApi(api = Build.VERSION_CODES.M)
public class CallHandler extends ConnectionService{

    public static final String TAG = CallHandler.class.getName();

    @RequiresApi(api = Build.VERSION_CODES.N_MR1)
    @Override
    public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
        // ... 其他操作 ...

        return callConnection;
    }

    // ... 其他操作 ...

}

在我的CallConnection.java中:

@RequiresApi(api = Build.VERSION_CODES.M)
public class CallConnection extends Connection {

    private Context context;

    @RequiresApi(api = Build.VERSION_CODES.N_MR1)
    public CallConnection(Context con) {
        // ... 其他操作 ...
    }

    @Override
    public void onAnswer(int videoState) {
        super.onAnswer(videoState);

        // ... 其他操作 ...
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onShowIncomingCallUi() {
        // ... 其他操作 ...
    }
}

你提供的代码涉及TelecomManager、ConnectionService和Connection的呼叫应用程序实现。在你的MainActivity.java中,你设置了默认的拨号应用程序,然后创建了一个自管理的PhoneAccount,并在有来电时添加了一个新的呼入通话。在CallHandler.java中,你重写了ConnectionService的方法,用于处理呼入和呼出通话的创建。在CallConnection.java中,你创建了CallConnection类,它继承自Connection,并且你在该类中处理了接听呼叫和显示呼入通话界面的逻辑。但是你的问题是在有来电时界面没有显示出来。

你提供的代码中,涉及到一些操作,例如设置通话属性、注册PhoneAccount等等。但在代码中,没有直接涉及incomingActivity UI的显示问题。所以问题可能存在于代码的其他部分,或者在调用这些代码的地方。

如果你想要解决呼入通话界面没有显示的问题,你需要仔细检查整个应用程序的逻辑,确保所有涉及界面显示的地方都正确地触发和处理了。可能需要检查清单文件中的Activity声明,确保你的IncomingCallScreenActivity被正确声明,并且与相关的Intent匹配。

另外,你提到日志消息在应用程序启动时显示出来,但在有来电时没有显示。这可能是因为日志消息在接听呼叫或显示界面时没有被正确调用,或者由于其他原因导致这些方法没有被触发。

综上所述,要解决呼入通话界面没有显示的问题,你需要仔细检查代码中涉及界面显示的部分,并确保相关的方法被正确地调用和处理。如果问题仍然存在,可能需要进一步调试和排除。

英文:

I am building basic calling app using TelecomManager, ConnectionService and Connection. But, When an there is an incoming call, my incomingActivity UI is not showing up. Below is the sample code so far.

In my MainActivity.java

Intent intent = new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER);
intent.putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, getPackageName());

startActivity(intent);

// ================================================================
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    TelecomManager manager = (TelecomManager) getSystemService(TELECOM_SERVICE);

//            new ComponentName(getPackageName(), CallHandler.TAG), "myCallHandlerId");
    PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
            new ComponentName(getApplicationContext(), CallHandler.TAG), "myCallHandlerId");

    PhoneAccount phoneAccount = PhoneAccount
                            .builder(phoneAccountHandle, "myCallHandlerId")
                            .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)
                            .build();

    manager.registerPhoneAccount(phoneAccount);

    Log.i("Phone Account", "" + manager.getPhoneAccount(phoneAccountHandle));
    Log.i("Phone Account", "" + manager.getPhoneAccount(phoneAccountHandle).isEnabled());
    Log.i("Phone Account", "" + manager.getPhoneAccount(phoneAccountHandle).getClass());

    Log.i("Phone Account isEnabled", "" + phoneAccount.isEnabled());

    Bundle bundle = new Bundle();

    Uri uri = Uri.fromParts(PhoneAccount.SCHEME_TEL, "555555555", null);
    bundle.putParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS, uri);
    bundle.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);

    //            manager.addNewIncomingCall(phoneAccountHandle, bundle);

    Log.i("Permitted", "" + manager.isIncomingCallPermitted(phoneAccountHandle));

    if(manager.isIncomingCallPermitted(phoneAccountHandle)){
        Log.i("Call", "Incoming");
        manager.addNewIncomingCall(phoneAccountHandle, bundle);

    }
}

In my CallHandler.java


@RequiresApi(api = Build.VERSION_CODES.M)
public class CallHandler extends ConnectionService{

    public static final String TAG = CallHandler.class.getName();

    @RequiresApi(api = Build.VERSION_CODES.N_MR1)
    @Override
    public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
        Log.i("CallHandler","onCreateIncomingConnection");

        //        return super.onCreateIncomingConnection(connectionManagerPhoneAccount, request);

        Context context = getApplicationContext();

        Log.i("Context","" + context);
        Log.i("Context","" + context.getPackageName());
        Log.i("Context","" + getBaseContext());
        Log.i("Context","" + context.getClass().getName());
        Log.i("Context","" + context.getClass().getSimpleName());


        CallConnection callConnection = new CallConnection(context);
        callConnection.setInitializing();
        callConnection.setActive();

        callConnection.setCallerDisplayName("Manik", TelecomManager.PRESENTATION_ALLOWED);
        //        callConnection.setConnectionProperties(Connection.PROPERTY_SELF_MANAGED);
        //        callConnection.setConnectionCapabilities(Connection.CAPABILITY_HOLD & Connection.CAPABILITY_SUPPORT_HOLD);


        return callConnection;
    }

    @Override
    public void onCreateIncomingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
        super.onCreateIncomingConnectionFailed(connectionManagerPhoneAccount, request);
    }

    @Override
    public void onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
        super.onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount, request);
    }

    @Override
    public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
        return super.onCreateOutgoingConnection(connectionManagerPhoneAccount, request);
    }
}

In my CallConnection.java

@RequiresApi(api = Build.VERSION_CODES.M)
public class CallConnection extends Connection {

private Context context;

@RequiresApi(api = Build.VERSION_CODES.N_MR1)
public CallConnection(Context con) {
    context = con;
    setConnectionProperties(PROPERTY_SELF_MANAGED);
    setAudioModeIsVoip(true);
}

@Override
public void onAnswer(int videoState) {
    super.onAnswer(videoState);

    Log.i("Call","Answered");
}

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onShowIncomingCallUi() {
    Log.i("Call","Incoming Call");
    super.onShowIncomingCallUi();

    //        MainActivity con = new MainActivity();
    //        Context context = con.getApplicationContext();

    NotificationChannel channel = new NotificationChannel("channel", "Incoming Calls",
            NotificationManager.IMPORTANCE_HIGH);
    channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
    // 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 = context.getSystemService(NotificationManager.class);
    //        mgr.createNotificationChannel(channel);


    // 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, IncomingCallScreenActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, 0);

    Log.i("Intent1","" + intent);
    Log.i("Intent2","" + intent.getPackage());
    Log.i("Intent3","" + intent.getType());
    Log.i("Intent4","" + intent.getData());
    Log.i("Intent5","" + intent.getDataString());
    Log.i("Intent6","" + intent.getAction());
    Log.i("Intent7","" + intent.getCategories());
    Log.i("Intent8","" + intent.getExtras());

    Log.i("Pending Intent","" + pendingIntent);
    Log.i("Pending Intent","" + pendingIntent.getCreatorPackage());

    // 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(R.mipmap.ic_launcher);
    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 = context.getSystemService(
            NotificationManager.class);
    notificationManager.notify("Call Notification", 37, notification);

    //        context.startActivity(intent);


    }
}

All the log messages inside onCreateIncomingConnection() and onShowIncomingCallUi() are showing up when the app launches, and not when there is an incoming call.

All the permissions in AndroidManifest.xml

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;uses-permission android:name=&quot;android.permission.READ_CONTACTS&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.READ_CALL_LOG&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.WRITE_CONTACTS&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.CALL_PHONE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.MANAGE_OWN_CALLS&quot; /&gt;
    &lt;!--
 Needed only if your calling app reads numbers from the `PHONE_STATE`
         intent action.
    --&gt;
    &lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot; /&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;activity android:name=&quot;.IncomingCallScreenActivity&quot;&gt;&lt;/activity&gt;
        &lt;activity android:name=&quot;.CallScreenActivity&quot; /&gt;
        &lt;activity android:name=&quot;.ContactsActivity&quot; /&gt;
        &lt;activity android:name=&quot;.LogsActivity&quot; /&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
                &lt;action android:name=&quot;android.intent.action.DIAL&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
                &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;

                &lt;data android:scheme=&quot;tel&quot; /&gt;
            &lt;/intent-filter&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.DIAL&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

        &lt;service
            android:name=&quot;.CallHandler&quot;
            android:permission=&quot;android.permission.BIND_TELECOM_CONNECTION_SERVICE&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.telecom.ConnectionService&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/service&gt;
    &lt;/application&gt;

<!-- end snippet -->

Any help would be appreciated. Thanks

huangapple
  • 本文由 发表于 2020年6月29日 13:30:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/62631787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定