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

阿里 阅读:237 2021-03-31 22:12:26 评论:0

我们在之前依次介绍openfire部署以及smack常用API的使用,这一节中我们着力介绍如何基于asmack开发一个Android的客户端,本篇的重点在实践,讲解和原理环节,大家可以参考前面我所发布的OpenFire和Smack的相关文章。今天所讲解的文章只是 :基于Smack3.0.4+ Openfire3.10.2开发之Android 客户端之四(消息会话、联系人、设置与XMPP 服务器交互)。

1、源代码结构介绍:


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

adapter包存放界面适配器

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

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

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

fragment包存放自定义fragment组件

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" 
             android:windowSoftInputMode="adjustPan" 
           /> 
           
           <service android:name="com.msn.service.MsfService" /> 
    </application> 
 
</manifest> 
2、核心代码介绍

主界面:MainActivity.java

package com.msn.activity; 
 
import com.msn.R; 
import com.msn.activity.LoginActivity; 
import com.msn.fragment.ConstactFragment; 
import com.msn.fragment.NewsFragment; 
import com.msn.fragment.SettingFragment; 
import com.msn.service.MsfService; 
 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.view.Gravity; 
import android.view.KeyEvent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.ImageButton; 
import android.widget.LinearLayout; 
import android.widget.PopupWindow; 
import android.widget.TextView; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Color; 
import android.graphics.drawable.ColorDrawable; 
 
public class MainActivity extends FragmentActivity implements OnClickListener { 
	protected static final String TAG = "MainActivity"; 
	private Context mContext; 
	private ImageButton mNews,mConstact,mSetting; 
	private View mPopView; 
	private View currentButton; 
	 
	private TextView app_cancle; 
	private TextView app_exit; 
	private TextView app_change; 
	 
	private PopupWindow mPopupWindow; 
	private LinearLayout buttomBarGroup; 
	 
	Fragment newsFatherFragment; 
	Fragment constactFatherFragment; 
	Fragment settingFragment; 
	 
	boolean isStartService=false; 
	 
	@Override 
	protected void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.activity_main); 
		mContext=this; 
		isStartService=getIntent().getBooleanExtra("isStartService", false); 
		if(isStartService){ 
			Intent intent=new Intent(mContext, MsfService.class); 
			startService(intent); 
		} 
		newsFatherFragment=(Fragment)getSupportFragmentManager().findFragmentById(R.id.fragment_news);//消息 
		constactFatherFragment=(Fragment) getSupportFragmentManager().findFragmentById(R.id.fragment_constact);//联系人 
		settingFragment=(Fragment) getSupportFragmentManager().findFragmentById(R.id.fragment_setting);//我 
		findView(); 
		init(); 
	} 
	 
	private void findView(){ 
		mPopView=LayoutInflater.from(mContext).inflate(R.layout.app_exit, null);//pop,手机菜单键弹出 
		 
		buttomBarGroup=(LinearLayout) findViewById(R.id.buttom_bar_group);//底部导航栏 
		mNews=(ImageButton) findViewById(R.id.buttom_news);//消息 
		mConstact=(ImageButton) findViewById(R.id.buttom_constact);//联系人 
		mSetting=(ImageButton) findViewById(R.id.buttom_setting);//我 
		 
		app_cancle=(TextView) mPopView.findViewById(R.id.app_cancle);//取消 
		app_change=(TextView) mPopView.findViewById(R.id.app_change_user);//注销用户 
		app_exit=(TextView) mPopView.findViewById(R.id.app_exit);//退出 
		 
	} 
	 
	private void init(){ 
		mNews.setOnClickListener(this); 
		mConstact.setOnClickListener(this); 
		mSetting.setOnClickListener(this); 
		mNews.performClick(); 
		initPop(); 
	} 
	 
	private void initPop() { 
        mPopupWindow=new PopupWindow(mPopView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true); 
		app_cancle.setOnClickListener(new OnClickListener() { 
			@Override 
			public void onClick(View v) { 
				mPopupWindow.dismiss(); 
			} 
		}); 
		app_change.setOnClickListener(new OnClickListener() { 
			@Override 
			public void onClick(View v) { 
				Intent intent=new Intent(mContext, LoginActivity.class); 
				startActivity(intent); 
				((Activity)mContext).overridePendingTransition(R.anim.activity_up, R.anim.fade_out); 
				try{ 
					MsfService.getInstance().stopSelf(); 
				}catch(Exception e){ 
					 
				} 
				finish(); 
			} 
		}); 
		app_exit.setOnClickListener(new OnClickListener() { 
			@Override 
			public void onClick(View v) { 
				try{ 
					MsfService.getInstance().stopSelf(); 
				}catch(Exception e){ 
					 
				} 
				finish(); 
			} 
		}); 
	} 
	 
	private void setButton(View v){ 
		if(currentButton!=null&¤tButton.getId()!=v.getId()){ 
			currentButton.setEnabled(true); 
		} 
		v.setEnabled(false); 
		currentButton=v; 
	} 
 
	@Override 
	public boolean onKeyDown(int keyCode, KeyEvent event) { 
		if(keyCode==KeyEvent.KEYCODE_MENU){//菜单键 
			mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#b0000000"))); 
			mPopupWindow.showAtLocation(buttomBarGroup, Gravity.BOTTOM, 0, 0); 
			mPopupWindow.setAnimationStyle(R.style.app_pop); 
			mPopupWindow.setOutsideTouchable(true); 
			mPopupWindow.setFocusable(true); 
			mPopupWindow.update(); 
		}else if(keyCode == KeyEvent.KEYCODE_BACK){ 
			moveTaskToBack(true); 
			return false; 
		} 
		return super.onKeyDown(keyCode, event); 
		 
	} 
 
	@Override 
	public void onClick(View v) { 
		switch (v.getId()) { 
		case R.id.buttom_news://消息 
			getSupportFragmentManager().beginTransaction().hide(constactFatherFragment).hide(settingFragment).show(newsFatherFragment).commit(); 
			setButton(v); 
			break; 
		case R.id.buttom_constact://联系人 
			getSupportFragmentManager().beginTransaction().hide(newsFatherFragment).hide(settingFragment).show(constactFatherFragment).commit(); 
			setButton(v); 
			break; 
		case R.id.buttom_setting://设置 
			getSupportFragmentManager().beginTransaction().hide(constactFatherFragment).hide(newsFatherFragment).show(settingFragment).commit(); 
			setButton(v); 
			break; 
		default: 
			break; 
		} 
	} 
     
} 
相关的资源文件,我就不一一做详细的述说,这里提供该项目的下载地址:源代码下载地址

3、相关展示:

一、客户端主界面展示:





声明

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

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号