1

Implementing twilio for calls , I try to use broadcast receiver instead of activity for the twilio logic. My broadcast receiver is supposed to catch an intent through onReceive(), but actually it doesn't. The call is heared ! but cannot catch the event and proceed within my activity

My broadcastreceiver (TwilioBroadCastReceiver) contains :

@Override
public void onReceive(Context context, Intent intent) {
        if (intent != null) {
        /*
         * Determine if the receiving Intent has an extra for the incoming connection. If so,
         * remove it from the Intent to prevent handling it again next time the Activity is resumed
         */
        Device device = intent.getParcelableExtra(Device.EXTRA_DEVICE);
        Connection incomingConnection = intent.getParcelableExtra(Device.EXTRA_CONNECTION);

        if (incomingConnection == null && device == null) {
            return;
        }
        intent.removeExtra(Device.EXTRA_DEVICE);
        intent.removeExtra(Device.EXTRA_CONNECTION);

        pendingConnection = incomingConnection;
        pendingConnection.setConnectionListener(this);
        showIncomingDialog();
    }
}

and

private void createDevice(String capabilityToken) {
    try {
        if (clientDevice == null) {
            clientDevice = Twilio.createDevice(capabilityToken, this);
            Intent intent = new Intent(mActivity, TwilioBroadcastReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mActivity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            clientDevice.setIncomingIntent(pendingIntent);
        } else {
            clientDevice.updateCapabilityToken(capabilityToken);
        }

    } catch (Exception e) {
        Log.e(TAG, "An error has occured updating or creating a Device: \n" + e.toString());
        Toast.makeText(mActivity, "Device error", Toast.LENGTH_SHORT).show();
    }
}

in my activity :

mReceiver = new TwilioBroadcastReceiver(this, clientProfile);

someone have an idea ? tks

Manifest , hummm.... seems something missing there ?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="client.twilio.com.quickstart">

<!-- needed for monitoring network availability -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<!-- needed for processing on the network -->
<uses-permission android:name="android.permission.INTERNET"/>

<!-- needed to enable/disable the speakerphone -->
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

<!-- needed to receive audio from microphone during a call -->
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".ClientActivity"
        android:screenOrientation="portrait"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <service android:name="com.twilio.client.TwilioClientService" android:exported="false" android:stopWithTask="true"/>
</application>


</manifest>
marc
  • 116
  • 8
  • Have you registered your broadcast in your activity? If not, check this post http://stackoverflow.com/questions/4805269/programmatically-register-a-broadcast-receiver – AndroidRuntimeException Dec 29 '16 at 13:41
  • I thought about this too . but I use Twilio Sdk which I suppose should do it for me with: PendingIntent pendingIntent = PendingIntent.getBroadcast(mActivity.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); – marc Dec 29 '16 at 14:07
  • .... besides this I don't have any filter specification to register it . – marc Dec 29 '16 at 14:08
  • post your manifest please – David Wasser Dec 29 '16 at 14:42
  • I added the manifest. i am gonna see if a add the broadcast in it i can get more success – marc Dec 29 '16 at 15:31

1 Answers1

2

You need to have the BroadcastReceiver in your manifest:

<receiver android:name=".TwilioBroadcastReceiver"/>

otherwise, the Twilio application cannot deliver the PendingIntent to it. You do not need any <intent-filter> for the <receiver> because you are providing an explicit Intent to trigger it.

David Wasser
  • 93,459
  • 16
  • 209
  • 274