2016-09-08 48 views
1

我是android開發新手。我正在使用sinch服務來打電話從應用程序到應用程序。我正在關注本教程https://github.com/sinch/android-app-app-calling-headers 我能夠通過通知屏幕活動onShouldSendPushNotification喚醒接收方電話的方法。在接收方電話中,我不知道如何啓動onIncomingCall方法CallClientListener接口在CallService。我嘗試將CallServiceGcmListenerService綁定,但不會觸發onIncomingCall。我一直在嘗試3-4天,但沒有得到正確的解決方案。使用sinch SDK在推送通知中接收來電Android

public class CallScreenActivity extends BaseActivity { 

static final String TAG = CallScreenActivity.class.getSimpleName(); 

private AudioPlayer mAudioPlayer; 
private Timer mTimer; 
private UpdateCallDurationTask mDurationTask; 
private String mCallId; 
private long mCallStart = 0; 
public String ReceiverGCMID,ReceiverName,SenderName,ReceiverID,SenderID,SenderGCMID; 
private TextView mCallDuration; 
private TextView mCallState; 
private TextView mCallerName; 

private class UpdateCallDurationTask extends TimerTask { 

    @Override 
    public void run() { 
     CallScreenActivity.this.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       updateCallDuration(); 
      } 
     }); 
    } 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.callscreen); 
    mAudioPlayer = new AudioPlayer(this); 
    mCallDuration = (TextView) findViewById(R.id.callDuration); 
    mCallerName = (TextView) findViewById(R.id.remoteUser); 
    mCallState = (TextView) findViewById(R.id.callState); 
    Button endCallButton = (Button) findViewById(R.id.hangupButton); 

    endCallButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      endCall(); 
     } 
    }); 
    mCallStart = System.currentTimeMillis(); 
    Intent intent = getIntent(); 
    mCallId = intent.getStringExtra("CALL_ID"); 

    ReceiverName = intent.getStringExtra("RECEIVER_NAME"); 
    SenderName = intent.getStringExtra("Sender_NAME"); 
    ReceiverID = intent.getStringExtra("ReceiverID"); 
    SenderID = intent.getStringExtra("SenderID"); 
    SenderGCMID = intent.getStringExtra("SenderGCMID"); 
    ReceiverGCMID = intent.getStringExtra("receiverGCMID"); 
} 

@Override 
public void onServiceConnected() { 
    Call call = getSinchServiceInterface().getCall(mCallId); 

    if (call != null) { 
     call.addCallListener(new SinchCallListener()); 
     mCallerName.setText("Calling " + ReceiverName); 
     mCallState.setText(call.getState().toString()); 
    } else { 
     Log.e(TAG, "Started with invalid callId, aborting."); 
     finish(); 
    } 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    mDurationTask.cancel(); 
    mTimer.cancel(); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    mTimer = new Timer(); 
    mDurationTask = new UpdateCallDurationTask(); 
    mTimer.schedule(mDurationTask, 0, 500); 
} 

@Override 
public void onBackPressed() { 
    // User should exit activity by ending call, not by going back. 
} 

private void endCall() { 
    mAudioPlayer.stopProgressTone(); 
    Call call = getSinchServiceInterface().getCall(mCallId); 
    if (call != null) { 
     call.hangup(); 
    } 
    finish(); 
} 

private String formatTimespan(long timespan) { 
    long totalSeconds = timespan/1000; 
    long minutes = totalSeconds/60; 
    long seconds = totalSeconds % 60; 
    return String.format(Locale.US, "%02d:%02d", minutes, seconds); 
} 

private void updateCallDuration() { 
    if (mCallStart > 0) { 
     mCallDuration.setText(formatTimespan(System.currentTimeMillis() - mCallStart)); 
    } 
} 

private class SinchCallListener implements CallListener { 

    @Override 
    public void onCallEnded(Call call) { 
     CallEndCause cause = call.getDetails().getEndCause(); 
     Log.e(TAG, "Call ended. Reason: " + cause.toString()); 
     mAudioPlayer.stopProgressTone(); 
     setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE); 
     String endMsg = "Call ended: " + call.getDetails().toString(); 
     Toast.makeText(CallScreenActivity.this, endMsg, Toast.LENGTH_LONG).show(); 
     endCall(); 
    } 

    @Override 
    public void onCallEstablished(Call call) { 
     Log.d(TAG, "Call established"); 
     mAudioPlayer.stopProgressTone(); 
     mCallState.setText(call.getState().toString()); 
     setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); 
     mCallStart = System.currentTimeMillis(); 
    } 

    @Override 
    public void onCallProgressing(Call call) { 
     Log.d(TAG, "Call progressing"); 
     mAudioPlayer.playProgressTone(); 
    } 

    @Override 
    public void onShouldSendPushNotification(Call call, List<PushPair> pushPairs) { 
     Log.e(TAG,"onShouldSendPushNotification"); 
     String gcmid = ReceiverGCMID; 
     String APIkey = "**********************"; 
     GCM gcm = new GCM(); 
     String messagetoSend = "talaqbacheacallreceivekahebanasdo"+",,,@@@[email protected]@U...,,,"+SenderID+",,,@@@[email protected]@U...,,,"+ReceiverID+",,,@@@[email protected]@U...,,,"+SenderName+",,,@@@[email protected]@U...,,,"+ReceiverName+",,,@@@[email protected]@U...,,,"+SenderGCMID+",,,@@@[email protected]@U...,,,"+ReceiverGCMID+",,,@@@[email protected]@U...,,,"+mCallId; 
     gcm.sendMessage(APIkey,gcmid,messagetoSend); 
    } 
} 

}

public class MessageAndCallReceiver extends GcmListenerService implements ServiceConnection{ 

private CallService.SinchServiceInterface mSinchServiceInterface; 
static final String TAG = MessageAndCallReceiver.class.getSimpleName(); 

@Override 
public void onMessageReceived(String from, Bundle data) { 
    Log.e(TAG,"onMessageReceived called"); 
    getApplicationContext().bindService(new Intent(this, CallService.class), this, Context.BIND_AUTO_CREATE); 
    String daata = data.getString("data"); 
    String[] parts = daata.split(",,,@@@[email protected]@U...,,,"); 
    if(parts[0].equals("talaqbacheacallreceivekahebanasdo")){ 

     Intent intent = new Intent(this,CallService.class); 
     startService(intent); 
     Log.e(TAG,"Call Received in notification"); 

    }else{ 
     Log.e(TAG,"Message Received in notification"); 

    } 

} 
@Override 
public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 
    if (CallService.class.getName().equals(componentName.getClassName())) { 
     mSinchServiceInterface = (CallService.SinchServiceInterface) iBinder; 
    } 
} 

@Override 
public void onServiceDisconnected(ComponentName componentName) { 
    if (CallService.class.getName().equals(componentName.getClassName())) { 
     mSinchServiceInterface = null; 
    } 
} 

}

public class CallService extends Service { 

private static final String APP_KEY = "******************"; 
private static final String APP_SECRET = "********************"; 
private static final String ENVIRONMENT = "sandbox.sinch.com"; 

public static final String LOCATION = "LOCATION"; 
public static final String CALL_ID = "CALL_ID"; 
static final String TAG = CallService.class.getSimpleName(); 

private SinchServiceInterface mSinchServiceInterface = new SinchServiceInterface(); 
private SinchClient mSinchClient; 
private String mUserId; 
private StartFailedListener mListener; 


@Override 
public void onCreate() { 
    super.onCreate(); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    return super.onStartCommand(intent, flags, startId); 

} 

@Override 
public void onDestroy() { 
    Log.e(TAG,"onDestroy"); 
    if (mSinchClient != null && mSinchClient.isStarted()) { 
     mSinchClient.terminate(); 
    } 
    super.onDestroy(); 
} 

public void start(String userName,Context context) { 
    Log.e(TAG,"start"); 
    if (mSinchClient == null) { 
     mUserId = userName; 
     mSinchClient = Sinch.getSinchClientBuilder().context(getApplicationContext()).userId(userName) 
       .applicationKey(APP_KEY) 
       .applicationSecret(APP_SECRET) 
       .environmentHost(ENVIRONMENT).build(); 

     mSinchClient.setSupportCalling(true); 
     mSinchClient.startListeningOnActiveConnection(); 

     mSinchClient.addSinchClientListener(new MySinchClientListener()); 
     mSinchClient.getCallClient().addCallClientListener(new SinchCallClientListener()); 
     mSinchClient.start(); 
    } 
} 

private void stop() { 
    Log.e(TAG,"stop"); 
    if (mSinchClient != null) { 
     mSinchClient.terminate(); 
     mSinchClient = null; 
    } 
} 

private boolean isStarted() { 
    return (mSinchClient != null && mSinchClient.isStarted()); 
} 

@Override 
public IBinder onBind(Intent intent) { 
    Log.e(TAG,"onBind"); 
    return mSinchServiceInterface; 
} 

public class SinchServiceInterface extends Binder { 

    public Call callPhoneNumber(String phoneNumber) { 
     return mSinchClient.getCallClient().callPhoneNumber(phoneNumber); 
    } 

    public Call callUser(String userId) { 
     return mSinchClient.getCallClient().callUser(userId); 
    } 

    public Call callUser(String userId, Map<String, String> headers) { 
     return mSinchClient.getCallClient().callUser(userId, headers); 
    } 

    public String getUserName() { 
     return mUserId; 
    } 

    public boolean isStarted() { 
     return CallService.this.isStarted(); 
    } 

    public void startClient(String userName) { 
     Log.e(TAG,"startClient"); 
     Context context = getApplicationContext(); 
     start(userName,context); 
    } 

    public void stopClient() { 
     Log.e(TAG,"stopClient"); 
     stop(); 
    } 

    public void setStartListener(StartFailedListener listener) { 
     mListener = listener; 
    } 

    public Call getCall(String callId) { 
     return mSinchClient.getCallClient().getCall(callId); 
    } 
} 

public interface StartFailedListener { 
    void onStartFailed(SinchError error); 
    void onStarted(); 
} 

public void startClient(String userName,Context context) { 
    Log.e(TAG,"startClient"); 
    start(userName,context); 
} 

private class MySinchClientListener implements SinchClientListener { 

    @Override 
    public void onClientFailed(SinchClient client, SinchError error) { 
     Log.e(TAG, "onClientFailed because "+ error.getMessage().toString()); 
     if (mListener != null) { 
      mListener.onStartFailed(error); 
     } 
     mSinchClient.terminate(); 
     mSinchClient = null; 
    } 

    @Override 
    public void onClientStarted(SinchClient client) { 
     Log.e(TAG, "onClientStarted"); 
     if (mListener != null) { 
      mListener.onStarted(); 
     } 
    } 

    @Override 
    public void onClientStopped(SinchClient client) { 
     Log.e(TAG, "onClientStopped"); 
    } 

    @Override 
    public void onLogMessage(int level, String area, String message) { 
     switch (level) { 
      case Log.DEBUG: 
       Log.d(TAG, message); 
       break; 
      case Log.ERROR: 
       Log.e(TAG, message); 
       break; 
      case Log.INFO: 
       Log.i(TAG, message); 
       break; 
      case Log.VERBOSE: 
       Log.v(TAG, message); 
       break; 
      case Log.WARN: 
       Log.w(TAG, message); 
       break; 
     } 
    } 

    @Override 
    public void onRegistrationCredentialsRequired(SinchClient client, 
      ClientRegistration clientRegistration) { 
    } 
} 

private class SinchCallClientListener implements CallClientListener { 

    @Override 
    public void onIncomingCall(CallClient callClient, Call call) { 
     Log.e(TAG, "Incoming call"); 
     Intent intent = new Intent(CallService.this, IncomingCallScreenActivity.class); 
     intent.putExtra(CALL_ID, call.getCallId()); 
     intent.putExtra(LOCATION, call.getHeaders().get("location")); 
     intent.putExtra("sndrName", call.getHeaders().get("sndrName")); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     getApplicationContext().startActivity(intent); 
    } 
} 

}

public class BaseActivity extends AppCompatActivity implements ServiceConnection { 

public CallService.SinchServiceInterface mSinchServiceInterface; 
static final String TAG = BaseActivity.class.getSimpleName(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getApplicationContext().bindService(new Intent(this, CallService.class), this, 
      BIND_AUTO_CREATE); 
} 

@Override 
public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 
    Log.e(TAG,"onServiceConnected"); 
    if (CallService.class.getName().equals(componentName.getClassName())) { 
     mSinchServiceInterface = (CallService.SinchServiceInterface) iBinder; 
     onServiceConnected(); 
    } 
} 

@Override 
public void onServiceDisconnected(ComponentName componentName) { 
    if (CallService.class.getName().equals(componentName.getClassName())) { 
     mSinchServiceInterface = null; 
     onServiceDisconnected(); 
    } 
} 

protected void onServiceConnected() { 
    // for subclasses 
} 

protected void onServiceDisconnected() { 
    // for subclasses 
} 

public CallService.SinchServiceInterface getSinchServiceInterface() { 
    return mSinchServiceInterface; 
} 

}

回答