基于Smack3.0.4+ Openfire3.10.2开发之Android 客户端之三解析

符号 阅读:227 2021-03-31 22:12:36 评论:0

源代码下载地址我们在之前依次介绍openfire部署以及smack常用API的使用,这一节中我们着力介绍如何基于asmack开发一个Android的客户端,本篇的重点在实践,讲解和原理环节,大家可以参考前面我所发布的OpenFire和Smack的相关文章。今天所讲解的文章只是 :基于Smack3.0.4+ Openfire3.10.2开发之Android 客户端之三(用户登入、用户注册与XMPP 服务器交互)。

1、源代码结构介绍:


activity包下存放一些android页面交互相关的控制程序,还有一个些公共帮助类

application包存放时应用程序相关信息

bean包存放应用程序实体对象

db包存放应用程序SQLite封装操作

listener监听器主要存放对XMPP 监听信息封装

server包主要存放应用程序后台服务(主要连接XMPP 服务器)

util中存放一些常用的工具类

view中一些和android的UI相关的显示控件


AndroidManifest.xml为android功能清单的配置文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.msn" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
    <uses-sdk 
        android:minSdkVersion="13" 
        android:targetSdkVersion="18" /> 
     
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_CALL_LOG" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 
    <uses-permission android:name="android.permission.CALL_PHONE" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 
     
    <uses-feature android:name="android.hardware.camera" /> 
    <uses-feature android:name="android.hardware.camera.autofocus" /> 
 
    <uses-permission android:name="android.permission.VIBRATE" /> 
    <uses-permission android:name="android.permission.FLASHLIGHT" /> 
 
    <application 
        android:name="com.msn.application.MsnApplication" 
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Holo.Light.NoActionBar" > 
        <activity 
            android:name="com.msn.activity.WelcomeActivity" 
            android:label="@string/app_name" > 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
          <activity 
            android:name="com.msn.activity.LoginActivity" 
            /> 
          <activity 
            android:name="com.msn.activity.RegisterActivity" 
           /> 
            <activity 
            android:name="com.msn.activity.MainActivity" 
           /> 
           
           <service android:name="com.msn.service.MsfService" /> 
    </application> 
 
</manifest> 


2、核心代码介绍

欢迎界面:WelcomeActivity

package com.msn.activity; 
 
import com.msn.R; 
import com.msn.util.PreferencesUtils; 
 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.text.TextUtils; 
import android.widget.ImageView; 
 
public class WelcomeActivity extends Activity { 
	// 
	protected static final String TAG = "WelcomeActivity"; 
	//WelcomeActivity 界面相关组件 
	private Context mContext; 
	private ImageView mImageView; 
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		// TODO Auto-generated method stub 
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.activity_welcome); 
		mContext = this; 
		PreferencesUtils.clearSharePre(this); 
		findView(); 
		init(); 
	} 
	private void findView() { 
		mImageView = (ImageView) findViewById(R.id.iv_welcome); 
	} 
	private void init() { 
		mImageView.postDelayed(new Runnable() { 
			@Override 
			public void run() { 
				Intent intent =null; 
				String username=PreferencesUtils.getSharePreStr(WelcomeActivity.this, "username"); 
				String pwd=PreferencesUtils.getSharePreStr(WelcomeActivity.this, "pwd"); 
				if(!TextUtils.isEmpty(username)&&!TextUtils.isEmpty(pwd)){ 
					intent = new Intent(mContext, MainActivity.class); 
					intent.putExtra("isStartService", true); 
				}else{ 
					intent = new Intent(mContext, LoginActivity.class); 
				} 
				startActivity(intent); 
				finish(); 
			} 
		},2000); 
		 
	} 
	 
} 

登入界面:LoginActivity

package com.msn.activity; 
 
import com.msn.R; 
import com.msn.service.MsfService; 
import com.msn.util.PreferencesUtils; 
import com.msn.util.ToastUtil; 
import com.msn.view.Const; 
import com.msn.view.LoadingDialog; 
import com.msn.view.TextURLView; 
 
import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.text.TextUtils; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RelativeLayout; 
 
public class LoginActivity extends Activity { 
	//LoginActivity界面相关控件 
	private Context mContext; 
	private RelativeLayout rl_user; 
	private Button mLogin; 
	private Button register; 
	private TextURLView mTextViewURL;	 
	private EditText account,password; 
	 
	//LoginActivity界面相关效果控件 
	private LoadingDialog loadDialog; 
	 
	//LoginActivity界面相关数据信息 
	String username;//用户名 
	String pwd;//密码 
	 
	//LoginActivity界面相关服务 
	private BroadcastReceiver receiver; 
	 
	 
	 
	 
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		// TODO Auto-generated method stub 
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.activity_login); 
		mContext=this; 
		loadDialog=new LoadingDialog(this); 
		loadDialog.setTitle("正在登录..."); 
		findView(); 
		initTvUrl(); 
		init(); 
		initReceiver(); 
	} 
	 
	//LoginActivity 核心代码(广播服务) 
	private void initReceiver() { 
		receiver = new BroadcastReceiver() { 
			@Override 
			public void onReceive(Context context, Intent intent) { 
				if(intent.getAction().equals(Const.ACTION_IS_LOGIN_SUCCESS)){ 
					if(loadDialog.isShowing()){ 
						loadDialog.dismiss(); 
					} 
					boolean isLoginSuccess=intent.getBooleanExtra("isLoginSuccess", false); 
					if(isLoginSuccess){//登录成功 
						Intent intent2=new Intent(mContext,MainActivity.class); 
						startActivity(intent2); 
						finish(); 
					}else{ 
						ToastUtil.showShortToast(mContext, "登录失败,请检您的网络是否正常以及用户名和密码是否正确"); 
					} 
				} 
			} 
 
		 
		}; 
		//注册广播接收者 
		IntentFilter mFilter = new IntentFilter(); 
		mFilter.addAction(Const.ACTION_IS_LOGIN_SUCCESS); 
		registerReceiver(receiver, mFilter); 
	} 
	 
	 
	 
	//activity_login.xml 实例化LoginActivity 界面相关组件 
	private void findView(){ 
		rl_user=(RelativeLayout) findViewById(R.id.rl_user); 
		mLogin=(Button) findViewById(R.id.login); 
		register=(Button) findViewById(R.id.register); 
		mTextViewURL=(TextURLView) findViewById(R.id.tv_forget_password); 
		 
		account=(EditText) findViewById(R.id.account); 
		password=(EditText) findViewById(R.id.password); 
		 
	} 
	//自定义控件TextViewURL 赋值文本信息 
	private void initTvUrl(){ 
		mTextViewURL.setText(R.string.forget_password); 
	} 
	//登入和注册绑定监听事件,登入界面搭配动画效果 
	private void init(){ 
		Animation anim=AnimationUtils.loadAnimation(mContext, R.anim.login_anim); 
		anim.setFillAfter(true); 
		rl_user.startAnimation(anim); 
		mLogin.setOnClickListener(loginOnClickListener); 
		register.setOnClickListener(registerOnClickListener); 
	} 
	 
	 
	/** 
	 * 登录 
	 */ 
	private OnClickListener loginOnClickListener=new OnClickListener() { 
		@Override 
		public void onClick(View v) { 
			doLogin(); 
		} 
	}; 
	 
	/** 
	 * 注册 
	 */ 
	private OnClickListener registerOnClickListener=new OnClickListener() { 
		@Override 
		public void onClick(View v) { 
			Intent intent=new Intent(mContext, RegisterActivity.class); 
			startActivity(intent); 
			 
		} 
	}; 
	 
	 
	@Override 
	public void onStart() { 
		// TODO Auto-generated method stub 
		super.onStart(); 
		String username=PreferencesUtils.getSharePreStr(this, "username");//用户名 
		String pwd=PreferencesUtils.getSharePreStr(this, "pwd");//密码 
		if(!TextUtils.isEmpty(username)){ 
			account.setText(username); 
		} 
		if(!TextUtils.isEmpty(pwd)){ 
			password.setText(pwd); 
		} 
	} 
	 
	void doLogin(){ 
		 username=account.getText().toString();//用户名 
		 pwd=password.getText().toString();//密码 
		if(TextUtils.isEmpty(username)){ 
			ToastUtil.showShortToast(mContext, "请输入您的账号"); 
			return; 
		} 
		if(TextUtils.isEmpty(pwd)){ 
			ToastUtil.showShortToast(mContext, "请输入您的密码"); 
			return; 
		} 
		PreferencesUtils.putSharePre(mContext, "username", username); 
		PreferencesUtils.putSharePre(mContext, "pwd", pwd); 
		 
		loadDialog.show();		 
		//启动核心Service 
		Intent intent=new Intent(this,MsfService.class); 
		startService(intent); 
		 
	} 
	 
	 
	@Override 
	public void onDestroy() { 
		// TODO Auto-generated method stub 
		super.onDestroy(); 
		//注销广播 
		unregisterReceiver(receiver); 
	} 
	 
	 
	 
 
} 


注册界面:RegisterActivity

package com.msn.activity; 
 
import org.jivesoftware.smack.XMPPConnection; 
import org.jivesoftware.smack.XMPPException; 
 
import com.msn.R; 
import com.msn.util.PreferencesUtils; 
import com.msn.util.ToastUtil; 
import com.msn.util.XmppConnectionManager; 
import com.msn.util.XmppUtil; 
import com.msn.view.LoadingDialog; 
import com.msn.view.TitleBarView; 
 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.text.TextUtils; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
 
public class RegisterActivity extends Activity { 
	private Context mContext; 
	private Button btn_complete; 
	private TitleBarView mTitleBarView; 
	private EditText et_name,et_password; 
	 
	private String account,password; 
	private LoadingDialog loadDialog; 
	 
	private XmppConnectionManager xmppConnectionManager; 
	 
	@SuppressLint("HandlerLeak") 
	private final Handler mHandler = new Handler() { 
		@Override 
		public void handleMessage(Message msg) { 
			super.handleMessage(msg); 
			if(loadDialog.isShowing()){ 
				loadDialog.dismiss(); 
			} 
			switch (msg.what) { 
			case 0: 
				ToastUtil.showLongToast(mContext, "注册失败"); 
				break; 
			case 1: 
				ToastUtil.showLongToast(mContext, "注册成功,请牢记您的账号和密码"); 
				PreferencesUtils.putSharePre(mContext, "username", account); 
				PreferencesUtils.putSharePre(mContext, "pwd", password); 
				finish(); 
				break; 
			case 2: 
				ToastUtil.showLongToast(mContext, "该昵称已被注册"); 
				break; 
			case 3: 
				ToastUtil.showLongToast(mContext, "注册失败"); 
				break; 
			case 4: 
				ToastUtil.showLongToast(mContext, "注册失败,请检查您的网络"); 
				break; 
			default: 
				break; 
			} 
		} 
	}; 
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		// TODO Auto-generated method stub 
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.activity_register_userinfo); 
		mContext=this; 
		xmppConnectionManager=XmppConnectionManager.getInstance(); 
		//对话框初始化 
		loadDialog=new LoadingDialog(this); 
		findView(); 
		initTitleView(); 
		init(); 
	} 
	 
	//相关组件初始化 
	private void findView(){ 
		mTitleBarView=(TitleBarView) findViewById(R.id.title_bar); 
		btn_complete=(Button) findViewById(R.id.register_complete); 
		 
		et_name=(EditText) findViewById(R.id.name);//账号 
		et_password=(EditText) findViewById(R.id.password);//密码 
		 
	} 
	 
	private void init(){ 
		btn_complete.setOnClickListener(completeOnClickListener); 
	} 
	 
	private void initTitleView(){ 
		mTitleBarView.setCommonTitle(View.VISIBLE, View.VISIBLE,View.GONE, View.GONE); 
		mTitleBarView.setTitleText(R.string.title_register_info); 
		mTitleBarView.setBtnLeft(R.drawable.fft, R.string.back); 
		mTitleBarView.setBtnLeftOnclickListener(new OnClickListener() {	 
			@Override 
			public void onClick(View v) { 
				finish(); 
			} 
		}); 
	} 
	 
	/** 
	 * 点击注册 
	 */ 
	private OnClickListener completeOnClickListener=new OnClickListener() { 
		@Override 
		public void onClick(View v) { 
			doReg(); 
		} 
	}; 
	 
	void doReg(){ 
		account=et_name.getText().toString(); 
		password=et_password.getText().toString(); 
		if(TextUtils.isEmpty(account)){ 
			ToastUtil.showLongToast(mContext, "请填写昵称"); 
			return; 
		} 
		if(TextUtils.isEmpty(password)){ 
			ToastUtil.showLongToast(mContext, "请填写密码"); 
			return; 
		} 
		loadDialog.setTitle("正在注册..."); 
		loadDialog.show(); 
		new Thread(new Runnable() { 
			@Override 
			public void run() { 
				XMPPConnection mXMPPConnection=xmppConnectionManager.init(); 
				try { 
					mXMPPConnection.connect(); 
					int result=XmppUtil.register(mXMPPConnection, account, password); 
					mHandler.sendEmptyMessage(result); 
				} catch (XMPPException e) { 
					e.printStackTrace(); 
					mHandler.sendEmptyMessage(4); 
				} 
			} 
		}).start(); 
	} 
	 
	 
 
} 

相关的资源文件,我就不一一做详细的述说,这里提供该项目的下载地址:源代码下载

3、相关展示:

一、客户端登入


二、客户端注册



xmpp 服务端截图:


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们