QuickContactBadge继承了ImageView,因此它的本质也是图片,也可以通过android:src属性指定它显示的图片。QuickContackBadge额外功能是:该图片可以关联到手机中指定联系人,当用户单击该图片时,系统将打开相应的联系人的联系方式界面。
可以调用如下方法进行关联:
1、assignContactFromEmail(String emailAddress, boolean lazyLookup):
2、assignContactFromPhone(String phoneNumber, boolean lazyLookup);
3、assignContactUri(Uri contactUri)
下面以一个例子来说明:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<QuickContactBadge
android:id="@+id/badge"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="/blog_article/@drawable/zgdx/index.html"/>
</LinearLayout>
package com.example.testquickconnection;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.QuickContactBadge;
public class MainActivity extends Activity {
private QuickContactBadge badge;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取QuickContactBadge组件
badge = (QuickContactBadge) findViewById(R.id.badge);
badge.assignContactFromPhone("10000", false);
}
}
运行结果:
点击该图标后:
在 XMPP通讯开发-弹出好友通信对话框的逻辑设计 中我们实现双击好友弹出聊天的对话框,那只是一个框架,而我们要真是的实现聊天需要重写JDialog里面的内容JPanel。这里我们可以根据好友聊天需要的基本的功能,我们需要使用的是ChatManager这个聊天管理器,然后与指定用户建立Chat session,之后就可以发送和接收消息了。这部分我们可以看官方的文档smack_3_3_0\documentation\messaging.html里面Messaging using Chats,根据官网给出的例子,我们可以知道需要好友的XMPP的地址,还有就是connection,所以我们在构造JPanel的时候,需要传递这两个参数。我们看一下代码:
public void setChatPanel(XMPPConnection conn, String frindsXmppAddress){
this.conn = conn;
this.frindsXmppAddress = frindsXmppAddress;
CreateChatM();
dateUtils = new DateUtils();
chatName.setText(frindsXmppAddress);
}
private void CreateChatM(){
if(conn != null){
chatManager = conn.getChatManager();
chat = chatManager.createChat(frindsXmppAddress,
new MessageListener(){
@Override
public void processMessage(Chat chat, Message msg) {
if(msg != null){
ChatMessage.append(dateUtils.getHM()+" "+frindsXmppAddress+": "+msg.getBody()+"\n");
}
}
});
}
}
双击部分代码如下:
ChatPanel chatPanel = new ChatPanel(); chatPanel.setChatPanel(conn, "123@zhangjie"); JDialog chatDialog = new JDialog(); chatDialog.setContentPane(chatPanel); chatDialog.setSize(501, 512); chatDialog.setVisible(true);初始化ChatPanel后,然后设置那两个相关的参数,同时创建对话信息,然后启动JDialog显示界面,在下面的对话框中填写要发送的信息,点击发送,就会将填写的信息发送给123@zhagnjie上面,点击按钮功能如下:
private void SendChatBtnMouseClicked(java.awt.event.MouseEvent evt) {
String Content = ChatContent.getText();
try {
chat.sendMessage(Content);
ChatMessage.append(dateUtils.getHM()+" 自己: "+Content+"\n");
ChatContent.setText("");
} catch (XMPPException ex) {
Logger.getLogger(ChatPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
另一个客户端我是用的是Spark进行测试的,效果图如下:
两个可以正常的通信。
源码项目:https://github.com/jwzhangjie/IChat_PC.git
在XMPP通讯开发-好友通信实现 中我们实现了好友间的信息通信,在这里我们实现好友间文件的传输,这部分内容可以查看smack_3_3_0\documentation\extensions\index.html,文件传输我们主要用到的是FileTransferManager和OutgoingFileTransfer,当然我们可以发送文件也可以接收文件,要实现接收文件我们需要FileTransferManager添加一个监听事件FileTransferListener,来监听好友发送的文件事件,首先我们实现如何发送文件,代码如下:
private void sendFile(File file, String fileName){
try {
if(JUID == null)
JUID = conn.getRoster().getEntry(frindsXmppAddress).getUser();
transfer = fileManager.createOutgoingFileTransfer(frindsXmppAddress+"/Spark 2.6.3");
transfer.sendFile(file, fileName);
while(!transfer.isDone()){
if(transfer.getStatus().equals(Status.error)){
System.out.println("ERROR!!! " + transfer.getError());
}else{
System.out.println(transfer.getStatus()+"进度 "+transfer.getProgress());
}
Thread.sleep(1000);
}
} catch (Exception ex) {
ex.printStackTrace();
}finally{
if(transfer.getStatus().equals(Status.error)){
ChatMessage.append(dateUtils.getHM()+" 自己: 传输出错"+"\n");
}else if(transfer.getStatus().equals(Status.complete)){
ChatMessage.append(dateUtils.getHM()+" 自己: "+fileName+" 传输完成\n");
}else if(transfer.getStatus().equals(Status.cancelled)){
ChatMessage.append(dateUtils.getHM()+frindsXmppAddress+" "+fileName+" 传输取消\n");
}
}
}
这里要注意的就是
transfer = fileManager.createOutgoingFileTransfer(frindsXmppAddress+"/Spark 2.6.3");函数的原型public OutgoingFileTransfer createOutgoingFileTransfer(String userID),其中的userID包含三部分,. A fully-qualified jabber ID consists of a node, a domain, and a resource, the user must be connected to the resource in order to be able to recieve the file transfer。其实也可以说两部分就是xmpp地址+对方客户端,一定要包含这几个,不然会报错的,效果图如下:
这个是发送文件,接收文件如何进行呢,代码如下:
private void initTransFile(){
fileManager = new FileTransferManager(conn);
fileManager.addFileTransferListener(new FileTransferListener(){
@Override
public void fileTransferRequest(FileTransferRequest request) {
int respone = JOptionPane.showConfirmDialog(null, frindsXmppAddress+"发送文件"+request.getFileName(), "发送文件", JOptionPane.YES_NO_OPTION);
if(respone == 0){
try {
// Accept it
IncomingFileTransfer transfer = request.accept();
transfer.recieveFile(new File(request.getFileName()));
} catch (XMPPException ex) {
Logger.getLogger(ChatPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}else{
request.reject();
}
}
});
}
我们注册FileTransferListener进行监听,效果图如下:
源码项目:https://github.com/jwzhangjie/IChat_PC.git