openfire+asmack搭建的安卓即时通讯(五) 15.4.12
这一篇博客其实是要昨天写的,但昨天做了作修改就停不下来了,这次的修改应该是前期开发的最终回了,其余的功能有空再做了,下周可能要做一些好玩的东西,敬请期待!
1.修改下Logo:(Just We)
http://romannurik.github.io/AndroidAssetStudio/ 可以用这个网站来做哦,上传个图片就可以哦!
2.添加欢迎页:
我自己画了个Just We的欢迎页
这里是添加欢迎页活动的代码,把程序的启动活动换为Open活动:
public class Open extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final View view = View.inflate(this, R.layout.open, null);
setContentView(view);
//渐变展示启动屏
AlphaAnimation start = new AlphaAnimation(0.3f,1.0f);
start.setDuration(2000);
view.startAnimation(start);
start.setAnimationListener(new Animation.AnimationListener()
{
@Override
public void onAnimationEnd(Animation arg0) {
Log.e("linc", "---start!");
try{
Intent intent = new Intent();
intent.setClass(Open.this,MainActivity.class);
Open.this.startActivity(intent);
Open.this.finish();//记得要关闭,因为我们根本就不会再回到欢迎页
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationStart(Animation animation) {}
}); }
}
3.对主界面进行了一些修改,说不上好看但也能看吧:
这里是布局文件,添加了图片,设置Actionbar为叠加模式,显示为透明Name和Password设置最大值行数为1,
Password使用了密文,还添加了一个控件可以用来替换是否显示密码更改了Button的大小。
下面是新的布局写法:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="?android:attr/actionBarSize" //这个是计算Actionbar的宽度,以便不是真的叠加了
android:background="@drawable/background"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:src="@drawable/logo"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:layout_below="@id/image"
android:id="@+id/tablelayout"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:stretchColumns="1"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Name:"
/>
<EditText
android:id="@+id/login_name"
android:hint="Input your Name "
android:maxLines="1"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:text="Password:"
/>
<EditText
android:id="@+id/login_password"
android:hint="Input your Password "
android:maxLines="1"
android:password="true"
android:layout_height="wrap_content"
/>
</TableRow>
</TableLayout>
<CheckBox
android:id="@+id/show"
android:layout_below="@id/tablelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示密码"/>
<CheckBox
android:id="@+id/sain"
android:layout_below="@id/tablelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示密码"/>
<Button
android:layout_below="@id/show"
android:layout_centerHorizontal="true"
android:id="@+id/buttonlogin"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="Login In"/>
</RelativeLayout>
这个是控制checkbox的代码:注册checkbox,然后设置他的监控器
CheckBox checkBox=(CheckBox)findViewById(R.id.show);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//设置为明文
}else{
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//设置为密文
}
}
});
添加了双击返回键退出软件的功能:
private long exitTime = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
if((System.currentTimeMillis()-exitTime) > 2000){
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
finish();
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
4.添加朋友活动,作为主活动之后的运行活动:
用一个listview进行装载
package com.lfk.webim; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; import com.lfk.webim.appli.user; public class friend extends Activity {
public static ArrayAdapter<String> mArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friend);
Intent intent = getIntent();
final String username = intent.getStringExtra("usename");
TextView textView=(TextView)findViewById(R.id.name);
textView.setText(username+"的朋友");
final ListView listView=(ListView)findViewById(R.id.friend_list);
mArrayAdapter= new ArrayAdapter<String>(this, R.layout.list_item);
listView.setAdapter(mArrayAdapter);
ClientConServer.findMan();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String temp= (String) ((TextView)arg1).getText();
Intent intent = new Intent();
user.FromName=temp+"@172.6.33.68/Smack";//这么些是因为Android的用户名格式就是这样的
user.FromName_=temp; //这里使用了全局变量,所以不需要向下一个活动穿什么参数了
intent.setClass(friend.this, useractivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(),
"Chat with " + temp,
Toast.LENGTH_SHORT).show();
mArrayAdapter.notifyDataSetChanged();
} });
}
public static Handler mhandler=new Handler()
{
public void handleMessage(android.os.Message message)
{
String temp=(String)message.obj;
friend.mArrayAdapter.add(temp);
}
}; }
这个活动会生成一个列表,这个列表就是我们之前打印的那个组成员,现在我们就可以选择跟谁说话了,而不用在代码里制订了:
这个是他的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="?android:attr/actionBarSize"
android:background="@drawable/background"
>
<TextView
android:id="@+id/name"
android:text="@string/friend"
android:textSize="22dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:layout_below="@id/name"
android:id="@+id/friend_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:textSize="18sp"
android:padding="5dp"
android:id="@+id/friend_name"
/>
很简单吧!
5.修改过的聊天详情页:
public class useractivity extends Activity {
private ListView listView;
public static ArrayAdapter<String> mConversationArrayAdapter;
private TextView text_out;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.useractivity);
listView = (ListView) findViewById(R.id.in);
TextView textView = (TextView) findViewById(R.id.username);
textView.setText("Talk with "+user.FromName_);
mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
listView.setAdapter(mConversationArrayAdapter);
//connect.closeConnection();
Button button=(Button)findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input=(EditText) findViewById(R.id.edit_text_out);
final String content=input.getText().toString();
String string= "ME"+":"+content;
android.os.Message mm=new android.os.Message();
mm.obj=string;
mhandle.handleMessage(mm);
try {
XMPPConnection connection = connect.getConnection();
ChatManager cm = connection.getChatManager();
Chat chat=cm.createChat(user.FromName, new MessageListener() {
@Override
public void processMessage(Chat chat, Message msg) {
msg.setBody(content);
Log.i("---", msg.getFrom() + "说:" + msg.getBody());
//添加消息到聊天窗口 ,
}
});
Message m = new Message();
m.setBody(content);
chat.sendMessage(m.getBody());
input.setText("");
} catch (XMPPException e) {
e.printStackTrace();
}
}
Handler mhandle= new Handler()
{
public void handleMessage(android.os.Message m) {
text_out=(TextView)findViewById(R.id.text_out);
String respond=(String)m.obj;
Log.i("---",respond);
mConversationArrayAdapter.add(respond);
}
};
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize"
android:background="@drawable/background"
>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="18dp"
android:textColor="@color/unfocused"
/>
<ListView
android:id="@+id/in"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:layout_weight="1"
android:transcriptMode="alwaysScroll"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText android:id="@+id/edit_text_out"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="bottom"
android:hint="说点什么呢?"
android:maxLines="3"
/>
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:text="sent"
android:layout_gravity="bottom"
android:background="#00000000"
/>
</LinearLayout>
</LinearLayout>
这就是效果图
6.修改了接收的:
private static Handler handler = new Handler(){
public void handleMessage(android.os.Message m) {
Message msg=new Message();
msg=(Message) m.obj;
//把从服务器获得的消息通过广播发送
//Intent intent = new Intent("192.168.252.1");
String[] message=new String[]{ msg.getFrom(), msg.getBody()};
System.out.println("==========收到消息 From==========="+message[0].toString());
System.out.println("==========收到消息 Body===========" + message[1].toString());
String s=msg.getFrom();
String s1=s.split("@")[0];
if(user.UserName.equals(message[0].toString()))
System.out.println("自己的消息就不打印了");
else
{
useractivity.mConversationArrayAdapter.add(s1 + "说:" + msg.getBody());
}
// intent.putExtra("message", message);
//context.sendBroadcast(intent);//发送广播
}
};
findMan函数进行了修改以搭配friend页的使用:
public static void findMan(){
//获取用户组、成员信息。
System.out.println("--------find start----------");
Roster roster = connect.con.getRoster();
Collection<RosterGroup> entriesGroup = roster.getGroups();
System.out.println("team:"+entriesGroup.size());
for(RosterGroup group: entriesGroup){
Collection<RosterEntry> entries = group.getEntries();
int temp=group.getEntryCount();
System.out.println("--------groupnumber--------" + "\n" + temp);
System.out.println("--------groupName--------" + "\n" + group.getName());
for (RosterEntry entry : entries) {
System.out.println("name:"+entry.getName());
String string2=entry.getName();
android.os.Message message_list = new android.os.Message();
message_list.obj=string2;
friend.mhandler.sendMessage(message_list);
}
}
System.out.println("--------find end--------");
//在登陆以后应该建立一个监听消息的监听器,用来监听收到的消息:
ChatManager chatManager = connect.con.getChatManager();
chatManager.addChatListener(new MyChatManagerListener());
}
7.全局变量增加的东西:(一个用来制定用户名,一个用来显示)
public class user extends Application {
/**
* 当前登录的用户名
*/
public static String UserName = "";
public static String UserName_ = "";
/**
* 当前与你聊天的用户名
*/
public static String FromName = "";
public static String FromName_ = "";
}
修改完两个人的对话就能写在listview里面了


openfire+asmack搭建的安卓即时通讯(五) 15.4.12的更多相关文章
- openfire+asmack搭建的安卓即时通讯(一) 15.4.7
最进开始做一些android的项目,除了一个新闻客户端的搭建,还需要一个实现一个即时通讯的功能,参考了很多大神成型的实例,了解到operfire+asmack是搭建简易即时通讯比较方便,所以就写了这篇 ...
- openfire+asmack搭建的安卓即时通讯(三) 15.4.9
(能用得上话的话求点赞=-=,我表达不好的话跟我说哦) 上一次我们拿到了服务器端的组数据和用户信息,这就可以为我们日后使用好友系统打下基础了! 但是光是拿到了这些东西我们怎么能够满足呢?我们一个即时通 ...
- openfire+asmack搭建的安卓即时通讯(七) 15.5.27
本地化之章! 往期传送门: 1.http://www.cnblogs.com/lfk-dsk/p/4398943.html 2.http://www.cnblogs.com/lfk-dsk/p/441 ...
- openfire+asmack搭建的安卓即时通讯(四) 15.4.10
之前的教程不知道你们成功了没,,,没成功可以问我啊=-= 第四篇博文是要实现发送消息的功能. 首先在我们登陆后的活动的layout里添加这样的两个控件,一个EditText和一个Button用于发送数 ...
- openfire+asmack搭建的安卓即时通讯(六) 15.4.16
啊啊啊啊啊啊啊啊,这东西越做越觉得是个深坑啊! 1.SharedPreferences.Editor的密码保存和自动登录: 首先还是从主界面开始,因为要提升一下用户体验自然要加入保存密码和自动登录的功 ...
- openfire+asmack搭建的安卓即时通讯(二) 15.4.9
上期没有放成果图呢!忘了=-=,这就是上次的成果图,textview里面会显示登陆的名字(这个是默认管理员帐号=-=) 好吧,登陆了服务器我们就有了交互的功能啦可以说是前进了一大步呢!下面能我们就要试 ...
- Openfire XMPP Smack RTC IM 即时通讯 聊天 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- XMPP(三)-安卓即时通讯客户端
由于时间原因,所以更新比较慢 ,还请大家谅解,此次是对上篇文章中的安卓客户端初级版本进行的一次更新优化,在这次更新后,就有那么一点样子了,可以拿的出手了,呵呵,还在关注的同学也可以及时下载更新.此次主 ...
- 急急如律令!火速搭建一个C#即时通信系统!(附源码分享——高度可移植!)
(2016年3月更:由于后来了解到GGTalk开源即时通讯系统,因此直接采用了该资源用于项目开发,在此对作者表示由衷的感谢!) —————————————————————————————————— 人 ...
随机推荐
- QQ视差特效和ListView侧滑删除
如图所示是效果图,当向下拉时,图片会被拉出来,松手后恢复.和ListView的侧滑删除 1.视差特效 首先图片是通过addHeaderView加上去的,所以在设置Adapter前先设置一个View ...
- spring中常用工具类介绍
http://www.cnblogs.com/langtianya/p/3875103.html 文件资源操作 Spring 定义了一个 org.springframework.core.io ...
- linux tcp/ip编程和windows tcp/ip编程差别以及windows socket编程详解
最近要涉及对接现有应用visual c++开发的tcp客户端,花时间了解了下windows下tcp开发和linux的差别,从开发的角度而言,最大的差别是头文件(早期为了推广尽可能兼容,后面越来越扩展, ...
- innerHtml and Jquery.html()
1. innerHtml是Dom HTML的属性 是只读的,不能写入. 2. JQuery只能调用.html(),它可以加参数,改变原HTML内容. http://api.jquery.com/htm ...
- Configure SSL for SharePoint 2013
http://blogs.msdn.com/b/fabdulwahab/archive/2013/01/21/configure-ssl-for-sharepoint-2013.aspx In thi ...
- Oracle LPAD/RPAD函数在处理中文时的注意事项
首先看下Oracle官方对函数的定义: The RPAD function returns an expression, right-padded to a specified length with ...
- iOS 核心动画
核心动画(Core Animation) : •CoreAnimation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.fr ...
- Mac显示隐藏文件的终端命令
显示:defaults write com.apple.finder AppleShowAllFiles -bool true隐藏:defaults write com.apple.finder Ap ...
- IOS中文版资源库
Swift 语言写成的项目会被标记为 ★ ,AppleWatch 的项目则会被标记为 ▲. [转自]https://github.com/jobbole/awesome-ios-cn#librari ...
- UVa 112 - Tree Summing(树的各路径求和,递归)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...