【发布时间】:2016-07-24 20:20:23
【问题描述】:
美好的一天。我正在使用 sinch 进行音频通话。我不知道该怎么做,也没有明确的 sinch 文档(这非常令人沮丧),这会让我知道如何让 sinch 客户端在后台运行在应用程序被杀死时在后台监听来电。同时我认为我强迫sinch客户端不被终止但每次应用程序打开时,客户端无论如何都会启动。所以如果有人遇到这样的事情,你能请帮助我并告诉我如何使用 Sinch 在后台监听来电?发布我已经启动并运行的代码。
我的所有活动都继承的基础活动。
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import ink.service.SinchService;
/**
* Created by USER on 2016-07-24.
*/
public abstract class BaseActivity extends AppCompatActivity implements ServiceConnection {
private SinchService.SinchServiceInterface mSinchServiceInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getApplicationContext().bindService(new Intent(this, SinchService.class), this,
BIND_AUTO_CREATE);
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
if (SinchService.class.getName().equals(componentName.getClassName())) {
mSinchServiceInterface = (SinchService.SinchServiceInterface) iBinder;
onServiceConnected();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Toast.makeText(BaseActivity.this, "Service disconnected", Toast.LENGTH_SHORT).show();
if (SinchService.class.getName().equals(componentName.getClassName())) {
mSinchServiceInterface = null;
onServiceDisconnected();
}
}
protected void onServiceConnected() {
// for subclasses
}
protected void onServiceDisconnected() {
// for subclasses
}
protected SinchService.SinchServiceInterface getSinchServiceInterface() {
return mSinchServiceInterface;
}
}
sinch 服务
* Created by USER on 2016-07-24.
*/
public class SinchService extends Service {
private static final String APP_KEY = "HIDDEN";
private static final String APP_SECRET = "HIDDEN";
private static final String ENVIRONMENT = "HIDDEN";
public static final String LOCATION = "LOCATION";
public static final String CALL_ID = "CALL_ID";
static final String TAG = SinchService.class.getSimpleName();
private SinchServiceInterface mSinchServiceInterface = new SinchServiceInterface();
private SinchClient mSinchClient;
private String mUserId;
private StartFailedListener mListener;
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(SinchService.this, "Service created", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
// if (mSinchClient != null && mSinchClient.isStarted()) {
// mSinchClient.terminate();
// }
Toast.makeText(SinchService.this, "Service destroyed", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
private void start(String userName) {
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.setSupportActiveConnectionInBackground(true);
mSinchClient.addSinchClientListener(new ClientListener());
mSinchClient.getCallClient().addCallClientListener(new SinchCallClientListener());
mSinchClient.start();
}
}
private void stop() {
if (mSinchClient != null) {
mSinchClient.terminate();
mSinchClient = null;
}
}
private boolean isStarted() {
return (mSinchClient != null && mSinchClient.isStarted());
}
@Override
public IBinder onBind(Intent intent) {
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 SinchService.this.isStarted();
}
public void startClient(String userName) {
start(userName);
}
public void 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();
}
private class ClientListener implements SinchClientListener {
@Override
public void onClientFailed(SinchClient client, SinchError error) {
if (mListener != null) {
mListener.onStartFailed(error);
}
mSinchClient.terminate();
mSinchClient = null;
}
@Override
public void onClientStarted(SinchClient client) {
Log.d(TAG, "SinchClient started");
if (mListener != null) {
mListener.onStarted();
}
}
@Override
public void onClientStopped(SinchClient client) {
Toast.makeText(SinchService.this, "Sinch client stopped", Toast.LENGTH_SHORT).show();
Log.d(TAG, "SinchClient stopped");
}
@Override
public void onLogMessage(int level, String area, String message) {
switch (level) {
case Log.DEBUG:
Log.d(area, message);
break;
case Log.ERROR:
Log.e(area, message);
break;
case Log.INFO:
Log.i(area, message);
break;
case Log.VERBOSE:
Log.v(area, message);
break;
case Log.WARN:
Log.w(area, message);
break;
}
}
@Override
public void onRegistrationCredentialsRequired(SinchClient client,
ClientRegistration clientRegistration) {
}
}
private class SinchCallClientListener implements CallClientListener {
@Override
public void onIncomingCall(CallClient callClient, Call call) {
Log.d(TAG, "Incoming call");
Intent intent = new Intent(SinchService.this, IncomingCallScreenActivity.class);
intent.putExtra(CALL_ID, call.getCallId());
intent.putExtra(LOCATION, call.getHeaders().get("callerName"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
SinchService.this.startActivity(intent);
}
}
}
这是我在启动器活动中启动 sinch 服务的方式。
@Override
protected void onServiceConnected() {
if (!getSinchServiceInterface().isStarted()) {
getSinchServiceInterface().startClient(mSharedHelper.getUserId());
}
getSinchServiceInterface().setStartListener(this);
}
【问题讨论】:
-
说真的???没人吗?
-
面临 smae 问题..找到任何解决方案了吗?
-
有谁找到解决方案的?
标签: android audio call sinch android-sinch-api