2016-12-29 38 views
1

爲呼叫實現twilio,我嘗試使用廣播接收器而不是twilio邏輯的活動。我的廣播接收器應該可以通過onReceive()獲得意圖,但事實上並非如此。 電話聽到了!但不能趕上事件,我的活動範圍內進行broadcastreceiver沒有用twilio發射

我的廣播接收器(TwilioBroadCastReceiver)包含:

@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(); 
    } 
} 

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

在我的活動:

mReceiver = new TwilioBroadcastReceiver(this, clientProfile); 

有人有想法? tks

Manifest,hummm ....似乎在那裏失蹤了?

<?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> 
+0

您是否在活動中註冊過廣播?如果沒有,請檢查這篇文章http://stackoverflow.com/questions/4805269/programmatically-register-a-broadcast-receiver – AndroidRuntimeException

+0

我也想過這個。但我使用Twilio Sdk,我認爲應該爲我做:PendingIntent pendingIntent = PendingIntent.getBroadcast(mActivity.getApplicationContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT); – marc

+0

....此外,我沒有任何過濾器規範註冊它。 – marc

回答

1

你需要在你的清單中BroadcastReceiver

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

否則,Twilio應用程序不能交付PendingIntent它。 對於<receiver>,您不需要任何<intent-filter>,因爲您提供了明確的Intent來觸發它。