Back to project page android-messaging-tutorial.
The source code is released under:
MIT License
If you think the Android project android-messaging-tutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.sinch.messagingtutorial.app; //from w w w. jav a 2 s . c o m import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.v4.content.LocalBroadcastManager; import com.parse.ParseUser; import com.sinch.android.rtc.ClientRegistration; import com.sinch.android.rtc.Sinch; import com.sinch.android.rtc.SinchClient; import com.sinch.android.rtc.SinchClientListener; import com.sinch.android.rtc.SinchError; import com.sinch.android.rtc.messaging.MessageClient; import com.sinch.android.rtc.messaging.MessageClientListener; import com.sinch.android.rtc.messaging.WritableMessage; public class MessageService extends Service implements SinchClientListener { private static final String APP_KEY = "key"; private static final String APP_SECRET = "secret"; private static final String ENVIRONMENT = "sandbox.sinch.com"; private final MessageServiceInterface serviceInterface = new MessageServiceInterface(); private SinchClient sinchClient = null; private MessageClient messageClient = null; private String currentUserId; private LocalBroadcastManager broadcaster; private Intent broadcastIntent = new Intent("com.sinch.messagingtutorial.app.ListUsersActivity"); @Override public int onStartCommand(Intent intent, int flags, int startId) { currentUserId = ParseUser.getCurrentUser().getObjectId(); if (currentUserId != null && !isSinchClientStarted()) { startSinchClient(currentUserId); } broadcaster = LocalBroadcastManager.getInstance(this); return super.onStartCommand(intent, flags, startId); } public void startSinchClient(String username) { sinchClient = Sinch.getSinchClientBuilder().context(this).userId(username).applicationKey(APP_KEY) .applicationSecret(APP_SECRET).environmentHost(ENVIRONMENT).build(); sinchClient.addSinchClientListener(this); sinchClient.setSupportMessaging(true); sinchClient.setSupportActiveConnectionInBackground(true); sinchClient.checkManifest(); sinchClient.start(); } private boolean isSinchClientStarted() { return sinchClient != null && sinchClient.isStarted(); } @Override public void onClientFailed(SinchClient client, SinchError error) { broadcastIntent.putExtra("success", false); broadcaster.sendBroadcast(broadcastIntent); sinchClient = null; } @Override public void onClientStarted(SinchClient client) { broadcastIntent.putExtra("success", true); broadcaster.sendBroadcast(broadcastIntent); client.startListeningOnActiveConnection(); messageClient = client.getMessageClient(); } @Override public void onClientStopped(SinchClient client) { sinchClient = null; } @Override public IBinder onBind(Intent intent) { return serviceInterface; } @Override public void onLogMessage(int level, String area, String message) { } @Override public void onRegistrationCredentialsRequired(SinchClient client, ClientRegistration clientRegistration) { } public void sendMessage(String recipientUserId, String textBody) { if (messageClient != null) { WritableMessage message = new WritableMessage(recipientUserId, textBody); messageClient.send(message); } } public void addMessageClientListener(MessageClientListener listener) { if (messageClient != null) { messageClient.addMessageClientListener(listener); } } public void removeMessageClientListener(MessageClientListener listener) { if (messageClient != null) { messageClient.removeMessageClientListener(listener); } } @Override public void onDestroy() { sinchClient.stopListeningOnActiveConnection(); sinchClient.terminate(); } public class MessageServiceInterface extends Binder { public void sendMessage(String recipientUserId, String textBody) { MessageService.this.sendMessage(recipientUserId, textBody); } public void addMessageClientListener(MessageClientListener listener) { MessageService.this.addMessageClientListener(listener); } public void removeMessageClientListener(MessageClientListener listener) { MessageService.this.removeMessageClientListener(listener); } public boolean isSinchClientStarted() { return MessageService.this.isSinchClientStarted(); } } }