不要使用sinch示例接收BroadcastReceiver

时间:2015-08-23 18:38:46

标签: android instant-messaging sinch

我尝试在我的应用中使用此示例,以便进行即时消息传递 https://www.sinch.com/tutorials/android-messaging-tutorial-using-sinch-and-parse/ 我呼叫广播接收器来监听我的服务广播,但它永远不会成功或失败。我的进度对话框一直在旋转,没有显示吐司。

我的服务:

public class MessageService extends Service implements SinchClientListener {
...
private LocalBroadcastManager broadcaster;
private Intent broadcastIntent = new Intent("il.ac.app.my.Activity.ListUsersActivity");
...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    APP_KEY = getResources().getString(R.string.sinch_key);
    APP_SECRET = getResources().getString(R.string.sinch_secret);
    ENVIRONMENT = getResources().getString(R.string.sinch_hostname);

    //get the current user id from Parse
    String currentUserId = ParseUser.getCurrentUser().getObjectId();
    if (currentUserId != null && !isSinchClientStarted()) {
        startSinchClient(currentUserId);
    }
    broadcaster = LocalBroadcastManager.getInstance(this);
    return super.onStartCommand(intent, flags, startId);
}
...
@Override
public void onClientStarted(SinchClient client) {
    broadcastIntent.putExtra("success", true);
    broadcaster.sendBroadcast(broadcastIntent);

    client.startListeningOnActiveConnection();
    messageClient = client.getMessageClient();
}

我的活动:

private void showSpinner() {
    progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("Loading");
    progressDialog.setMessage("Please wait...");
    progressDialog.show();

    //broadcast receiver to listen for the broadcast from MessageService
    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Boolean success = intent.getBooleanExtra("success", false);
            progressDialog.dismiss();
            if (!success) {
                Toast.makeText(getApplicationContext(), "Messaging service failed to start", Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(getApplicationContext(), "Messaging service started", Toast.LENGTH_LONG).show();
            }
        }
    };

    LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("il.ac.app.my.Activity.ListUsersActivity"));
}

2 个答案:

答案 0 :(得分:2)

在LoginActivity中,尝试在Sinch Service之前启动ListUsersActivity,如下所示:

 startActivity(intent);
 startService(serviceIntent);

答案 1 :(得分:0)

我尝试在我的应用程序中使用此示例以获得即时消息https://www.sinch.com/tutorials/android-messaging-tutorial-using-sinch-and-parse/我呼叫广播接收器以监听来自我的服务的广播,但它永远不会成功或失败。我的进度对话框一直在旋转,没有显示吐司。

我的服务:

public class MessageService extends Service implements SinchClientListener {
...
private LocalBroadcastManager broadcaster;
private Intent broadcastIntent = new Intent("il.ac.app.my.Activity.ListUsersActivity");
...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    APP_KEY = getResources().getString(R.string.sinch_key);
    APP_SECRET = getResources().getString(R.string.sinch_secret);
    ENVIRONMENT = getResources().getString(R.string.sinch_hostname);

    //get the current user id from Parse
    String currentUserId = ParseUser.getCurrentUser().getObjectId();
    if (currentUserId != null && !isSinchClientStarted()) {
        startSinchClient(currentUserId);
    }
    broadcaster = LocalBroadcastManager.getInstance(this);
    return super.onStartCommand(intent, flags, startId);
}
...
@Override
public void onClientStarted(SinchClient client) {
    broadcastIntent.putExtra("success", true);
    broadcaster.sendBroadcast(broadcastIntent);

    client.startListeningOnActiveConnection();
    messageClient = client.getMessageClient();
}