之前做够在线及时聊天,小型企业基本上都是通过xmpp协议实现,但是我之前公司做的多多少少会出现一些问题,今天在查找资料的时候,无意发现了一个很好的东西,某公司开发了一套即时聊天sdk,虽然也是由xmpp协议开发,但是稳定性和实现简答方面确实容易得多,通俗易懂。关于公司名字不说了,大家自己去搜吧,不然等会有打广告嫌疑!
涵盖了android和ios,下面给出简单的sdk介绍:
Android SDK 使用指南
首先将libs文件夹下的appkefu_sdk.jar加入到项目工程libs文件夹下(如果没有,则创建文件夹,仅针对eclipse开发环境). 其次,将res文件夹下所有文件拷贝到项目res文件夹下。在项目中使用appkefu_sdk.jar主要有两种方式,具体可参考示例程序AppKeFuDemoSimple和AppKeFuDemoAdvanced, 二者的主要区别在于前者每次会话的时候都会尝试与服务器建立新的会话,当关闭聊天窗口的时候关闭会话,断开与服务器的链接。后者则尝试在项目启动的时候 建立与服务器的一次性会话,在项目运行期间,所有的会话窗口公用此会话链接,直到项目结束运行或者网络故障AppKeFuDemoSimple 调用此方法启动会话窗口
/*** @param kefuName 客服的用户名*/private void startChat(String kefuName) {Log.d(TAG, "startChat:"+kefuName);String jid = kefuName + "@appkefu.com";Intent intent = new Intent(this, ChatActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);Contact contact = new Contact(jid);intent.setData(contact.toUri());startActivity(intent); }
AppKeFuDemoSimple 在AndroidManifest.xml中添加:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.VIBRATE" />
和
<activity android:name="com.appkefu.lib.ChatActivity" android:theme="@android:style/Theme.NoTitleBar" /> <service android:name="com.appkefu.lib.service.AppService" />
AppKeFuDemoAdvanced 主要分为四部分:AppKeFuApplication,MainActivity,LoginActivity和AndroidManifest.xml
import android.app.Application; /** * @author Administrator * */ public class AppKeFuApplication extends Application { private boolean mIsConnected = false; public AppKeFuApplication(){ } @Override public void onCreate() { super.onCreate(); } @Override public void onTerminate() { super.onTerminate(); } public boolean isConnected() { return mIsConnected; } public void setConnected(boolean isConnected) { mIsConnected = isConnected; } }
import com.appkefu.lib.ChatViewActivity;import com.appkefu.lib.service.Contact; import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG = MainActivity.class.getSimpleName();private static final int LOGIN_REQUEST_CODE = 1; private Button chatAdmin;private Button chatKeFu1;private Button chatKeFu2; private AppKeFuApplication app; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);chatAdmin = (Button)findViewById(R.id.chat_admin);chatAdmin.setOnClickListener(listener);chatKeFu1 = (Button)findViewById(R.id.chat_kefu1);chatKeFu1.setOnClickListener(listener);chatKeFu2 = (Button)findViewById(R.id.chat_kefu2);chatKeFu2.setOnClickListener(listener);app = (AppKeFuApplication)getApplication();} @Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}@Overrideprotected void onStart() {super.onStart();Log.d(TAG, "onStart");if(!app.isConnected()){Log.d(TAG, "start login");Intent login = new Intent(this, LoginActivity.class);startActivityForResult(login, LOGIN_REQUEST_CODE);}else{Log.d(TAG, "already logged in");} }@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == LOGIN_REQUEST_CODE) {if (resultCode == Activity.RESULT_OK){Log.d(TAG, "Activity.RESULT_OK");app.setConnected(true); }else if (resultCode == Activity.RESULT_CANCELED){Log.d(TAG, "Activity.RESULT_CANCELED");app.setConnected(false);chatAdmin.setEnabled(false);chatKeFu1.setEnabled(false);chatKeFu2.setEnabled(false);Toast.makeText(this, "登录失败", Toast.LENGTH_LONG).show();}}}@Overrideprotected void onRestart() {super.onRestart();Log.d(TAG, "onRestart");}@Overrideprotected void onResume() {super.onResume();Log.d(TAG, "onResume");}@Overrideprotected void onPause() {super.onPause();Log.d(TAG, "onPause");}@Overrideprotected void onStop() {super.onStop(); Log.d(TAG, "onStop");}@Overrideprotected void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy");} private OnClickListener listener = new OnClickListener() { @Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.chat_admin:startChat("admin");break;case R.id.chat_kefu1:startChat("kefu1");break;case R.id.chat_kefu2:startChat("kefu2");break;default:break;}}};private void startChat(String kefuName) {String jid = kefuName + "@appkefu.com";Intent intent = new Intent(this, ChatViewActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);Contact contact = new Contact(jid);intent.setData(contact.toUri());startActivity(intent);}
import com.appkefu.lib.service.AppService;import com.appkefu.lib.service.LoginAsyncTask;import com.appkefu.lib.service.XmppFacade; import android.os.AsyncTask;import android.os.Bundle;import android.os.IBinder;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.util.Log;import android.view.Menu; public class LoginActivity extends Activity {private static final String TAG = LoginActivity.class.getSimpleName();private XmppFacade mXmppFacade;private AsyncTask mLoginTask;private boolean mBinded = false;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_splash);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_splash, menu);return true;}@Overrideprotected void onStart() {super.onStart();Log.d(TAG, "onStart");if(mLoginTask == null)mLoginTask = new LoginTask();if (!mBinded) {Intent intent = new Intent(LoginActivity.this, AppService.class);bindService(intent, conn, Context.BIND_AUTO_CREATE);}}@Overrideprotected void onRestart() {super.onRestart();Log.d(TAG, "onRestart"); }@Overrideprotected void onResume() {super.onResume();Log.d(TAG, "onResume");}@Overrideprotected void onPause() {super.onPause();Log.d(TAG, "onPause");}@Overrideprotected void onStop() {super.onStop();if (mBinded) {unbindService(conn);mBinded = false;}mXmppFacade = null;//newChat = null;Log.d(TAG, "onStop");}@Overrideprotected void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy");}private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubLog.d(TAG, "MainActivity.onServiceDisconnected");}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubLog.d(TAG, "MainActivity.onServiceConnected");mXmppFacade = (XmppFacade) service;mLoginTask.execute(mXmppFacade);mBinded = true;}};private class LoginTask extends LoginAsyncTask {LoginTask() {Log.d(TAG, "LoginTask.Construction");}@Overrideprotected void onPostExecute(Boolean result) {Log.d(TAG, "LoginTask.onPostExecute");if (result == null || !result) { // Task cancelled or exceptionif (!result) {Intent i = new Intent();i.putExtra("message", getErrorMessage());LoginActivity.this.setResult(Activity.RESULT_CANCELED, i);} elseLoginActivity.this.setResult(Activity.RESULT_CANCELED);LoginActivity.this.finish();}else{//login succeedLog.d(TAG, "LoginTask.onPostExecute.true ");LoginActivity.this.startService(new Intent(LoginActivity.this, AppService.class));LoginActivity.this.setResult(RESULT_OK);LoginActivity.this.finish();}}@Overrideprotected void onProgressUpdate(Integer... values) {Log.d(TAG, "LoginTask.onProgressUpdate");}@Overrideprotected void onCancelled() {super.onCancelled();Log.d(TAG, "LoginTask.onCancelled");Intent intent = new Intent(LoginActivity.this, AppService.class);LoginActivity.this.stopService(intent);}}}
在Application标签中加
android:name="com.appkefu.demo.advanced.AppKeFuApplication"
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.VIBRATE" />
和
<activity android:name="com.appkefu.lib.ChatViewActivity" android:theme="@android:style/Theme.NoTitleBar" /> <service android:name="com.appkefu.lib.service.AppService" />
更多详细资料链接点击查看
官方Demo下载:http://download.csdn.net/detail/gulaer/6338067