不要使用 sinch 示例接收 BroadcastReceiver

Don't receive BroadcastReceiver using sinch example

我尝试在我的应用程序中使用这个示例来进行即时消息传递 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();
}

我的activity:

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

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

 startActivity(intent);
 startService(serviceIntent);

我尝试在我的应用程序中使用此示例以获得即时消息 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();
}