Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 輕松實現在線即時聊天[圖片、語音、表情、文字]等!含源碼!

android 輕松實現在線即時聊天[圖片、語音、表情、文字]等!含源碼!

編輯:關於Android編程

之前做夠在線及時聊天,小型企業基本上都是通過xmpp協議實現,但是我之前公司做的多多少少會出現一些問題,今天在查找資料的時候,無意發現了一個很好的東西,某公司開發了一套即時聊天sdk,雖然也是由xmpp協議開發,但是穩定性和實現簡答方面確實容易得多,通俗易懂。關於公司名字不說了,大家自己去搜吧,不然等會有打廣告嫌疑!     涵蓋了android和ios,下面給出簡單的sdk介紹: Android SDK 使用指南      首先將libs文件夾下的appkefu_sdk.jar加入到項目工程libs文件夾下(如果沒有,則創建文件夾,僅針對eclipse開發環境). 其次,將res文件夾下所有文件拷貝到項目res文件夾下。在項目中使用appkefu_sdk.jar主要有兩種方式,具體可參考示例程序AppKeFuDemoSimple和AppKeFuDemoAdvanced, 二者的主要區別在於前者每次會話的時候都會嘗試與服務器建立新的會話,當關閉聊天窗口的時候關閉會話,斷開與服務器的鏈接。後者則嘗試在項目啟動的時候 建立與服務器的一次性會話,在項目運行期間,所有的會話窗口公用此會話鏈接,直到項目結束運行或者網絡故障      AppKeFuDemoSimple 調用此方法啟動會話窗口   [html]   <span style="font-family:Microsoft YaHei; font-size:14px">/**   * @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);     }</span>     AppKeFuDemoSimple 在AndroidManifest.xml中添加:   [html]   <span style="font-family:Microsoft YaHei; font-size:14px">    <uses-permission android:name="android.permission.INTERNET" />       <uses-permission android:name="android.permission.VIBRATE" /></span>     和   [html]   <span style="font-family:Microsoft YaHei; font-size:14px">    <activity android:name="com.appkefu.lib.ChatActivity" android:theme="@android:style/Theme.NoTitleBar" />       <service android:name="com.appkefu.lib.service.AppService" /></span>     AppKeFuDemoAdvanced 主要分為四部分:AppKeFuApplication,MainActivity,LoginActivity和AndroidManifest.xml [html]  <span style="font-family:Microsoft YaHei; font-size:14px">    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;       }       }   </span>     [html]   <span style="font-family:Microsoft YaHei; font-size:14px">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);   } </span>     [html]  <span style="font-family:Microsoft YaHei; font-size:14px">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);   }   }   } </span>     在Application標簽中加   [html]   <span style="font-family:Microsoft YaHei; font-size:14px">android:name="com.appkefu.demo.advanced.AppKeFuApplication"</span>     [html]   <span style="font-family:Microsoft YaHei; font-size:14px">    <uses-permission android:name="android.permission.INTERNET" />       <uses-permission android:name="android.permission.VIBRATE" /></span>     和 [html]   <span style="font-family:Microsoft YaHei; font-size:14px">    <activity android:name="com.appkefu.lib.ChatViewActivity" android:theme="@android:style/Theme.NoTitleBar" />       <service android:name="com.appkefu.lib.service.AppService" /></span>    
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved