不废话了,直接开始,导包:
implementation "org.igniterealtime.smack:smack-android-extensions:4.3.0" implementation "org.igniterealtime.smack:smack-tcp:4.3.0" implementation 'org.igniterealtime.smack:smack-im:4.3.0' implementation 'org.igniterealtime.smack:smack-android:4.3.0'
使用:
-
package com.airiche.ymlworkingspace
-
-
import android.os.Bundle
-
import android.util.Base64
-
import android.util.Log
-
import androidx.appcompat.app.AppCompatActivity
-
import kotlinx.android.synthetic.main.activity_main.*
-
import org.jivesoftware.smack.*
-
import org.jivesoftware.smack.filter.StanzaTypeFilter.MESSAGE
-
import org.jivesoftware.smack.packet.Message
-
import org.jivesoftware.smack.packet.Stanza
-
import org.jivesoftware.smack.tcp.XMPPTCPConnection
-
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration
-
import org.jivesoftware.smack.util.TLSUtils
-
-
-
class MainActivity : AppCompatActivity() {
-
-
override
fun onCreate(savedInstanceState: Bundle?) {
-
super.onCreate(savedInstanceState)
-
setContentView(R.layout.activity_main)
-
SmackConfiguration.DEBUG =
true;
-
val config = XMPPTCPConnectionConfiguration.builder()
-
config.setUsernameAndPassword(
"2",
"12345678")
-
.setXmppDomain(
"xxx.net")
-
.setConnectTimeout(
30000)
-
-
var mBuild =
-
TLSUtils.acceptAllCertificates(
-
TLSUtils.disableHostnameVerificationForTlsCertificates(
-
config
-
)
-
).build();
-
-
val conn2: AbstractXMPPConnection = XMPPTCPConnection(mBuild)
-
conn2.replyTimeout =
30000
-
var runnable =
object : Runnable {
-
override
fun run() {
-
try {
-
conn2.connect().login()
-
}
catch (e: Exception) {
-
Log.e(
"TAG",
" conn2.connect()Exception= " + e.message)
-
}
-
}
-
}
-
Thread(runnable).start()
-
-
// val chatManager: ChatManager = ChatManager.getInstanceFor(conn2)
-
// chatManager.addIncomingListener(IncomingChatMessageListener { from, message, chat ->
-
// chat.send(message.getBody());
-
// Log.e("TAG", "New message from " + from + ": " + message.body)
-
// })
-
-
conn2.addAsyncStanzaListener(
object : StanzaListener {
-
override
fun processStanza(packet: Stanza?) {
-
//连接成功以后的数据回调
-
val headlineMessage = packet
as Message
-
Log.e(
"TAG",
"headlineMessage======" + headlineMessage)
-
-
}
-
}, MESSAGE)
-
-
-
conn2.addConnectionListener(
object : ConnectionListener {
-
override
fun connected(connection: XMPPConnection?) {
-
Log.e(
"TAG",
"connected()=== " + connection.toString())
-
-
}
-
-
override
fun connectionClosed() {
-
Log.e(
"TAG",
"connectionClosed()=== ")
-
}
-
-
override
fun connectionClosedOnError(e: Exception?) {
-
if (e !=
null) {
-
Log.e(
"TAG",
"connectionClosedOnError()=== " + e.message)
-
}
-
}
-
-
override
fun authenticated(connection: XMPPConnection?, resumed: Boolean) {
-
Log.e(
"TAG",
"authenticated()=== " + resumed)
-
Log.e(
"TAG",
" mConnection.isAuthenticated();=======" + conn2.isAuthenticated())
-
}
-
-
})
-
-
logins.setOnClickListener {
-
var runnable =
object : Runnable {
-
override
fun run() {
-
Log.e(
"TAG",
"连接状态=" + conn2.isConnected)
-
if (!conn2.isConnected) {
-
try {
-
conn2.connect()
-
}
catch (e: Exception) {
-
Log.e(
"TAG",
" conn2.connect() Exception= " + e.message)
-
}
-
}
-
}
-
}
-
Thread(runnable).start()
-
}
-
-
disLogin.setOnClickListener {
-
var runnable =
object : Runnable {
-
override
fun run() {
-
conn2.disconnect()
-
}
-
}
-
Thread(runnable).start()
-
}
-
}
-
}
介绍:看文档
转载:https://blog.csdn.net/qq_36333309/article/details/111480184
查看评论