5

我正在使用 TelecomManager、ConnectionService 和 Connection 构建基本呼叫应用程序。但是,当有来电时,我的incomingActivity UI 没有出现。以下是到目前为止的示例代码。

在我的 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);

    }
}

在我的 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);
    }
}

在我的 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);


    }
}

onCreateIncomingConnection()里面的所有日志消息都会在onShowIncomingCallUi()应用程序启动时显示,而不是在有来电时显示。

AndroidManifest.xml 中的所有权限

<uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />
    <!--
 Needed only if your calling app reads numbers from the `PHONE_STATE`
         intent action.
    -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.DIAL" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="tel" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.DIAL" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <service
            android:name=".CallHandler"
            android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
            <intent-filter>
                <action android:name="android.telecom.ConnectionService" />
            </intent-filter>
        </service>
    </application>

任何帮助,将不胜感激。谢谢

4

0 回答 0