android 轻松实现在线即时聊天【图片、语音、表情、文字】等!含源码!

     之前做够在线及时聊天,小型企业基本上都是通过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;
 
@Override
protected 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();
}
 
@Override
public 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;
}
@Override
protected 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");
}
 
}
@Override
protected 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();
}
}
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
 
Log.d(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
 
private OnClickListener listener = new OnClickListener() {	
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
}
@Override
public 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;
}
@Override
protected 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);
}
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart");	
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
if (mBinded) {
unbindService(conn);
mBinded = false;
}
mXmppFacade = null;
//newChat = null;
Log.d(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
Log.d(TAG, "MainActivity.onServiceDisconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.d(TAG, "MainActivity.onServiceConnected");
mXmppFacade = (XmppFacade) service;
mLoginTask.execute(mXmppFacade);
mBinded = true;
}
};
private class LoginTask extends LoginAsyncTask {
LoginTask() {
Log.d(TAG, "LoginTask.Construction");
}
@Override
protected void onPostExecute(Boolean result) {
Log.d(TAG, "LoginTask.onPostExecute");
if (result == null || !result) { // Task cancelled or exception
if (!result) {
Intent i = new Intent();
i.putExtra("message", getErrorMessage());
LoginActivity.this.setResult(Activity.RESULT_CANCELED, i);
} else
LoginActivity.this.setResult(Activity.RESULT_CANCELED);
LoginActivity.this.finish();
}
else
{
//login succeed
Log.d(TAG, "LoginTask.onPostExecute.true ");
LoginActivity.this.startService(new Intent(LoginActivity.this, AppService.class));
LoginActivity.this.setResult(RESULT_OK);
LoginActivity.this.finish();
}
}
@Override
protected void onProgressUpdate(Integer... values) {
Log.d(TAG, "LoginTask.onProgressUpdate");
}
@Override
protected 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


上一篇:运用OpenGL ES 2.0实现各种各样图像滤镜(图像处理)效果,多达50多种效果。


下一篇:LLVM每日谈之十一 编译器相关学习资料推荐