飞道的博客

安卓开发之MXPP的简单使用

209人阅读  评论(0)

不废话了,直接开始,导包:


   
  1. implementation "org.igniterealtime.smack:smack-android-extensions:4.3.0"
  2. implementation "org.igniterealtime.smack:smack-tcp:4.3.0"
  3. implementation 'org.igniterealtime.smack:smack-im:4.3.0'
  4. implementation 'org.igniterealtime.smack:smack-android:4.3.0'

使用:


  
  1. package com.airiche.ymlworkingspace
  2. import android.os.Bundle
  3. import android.util.Base64
  4. import android.util.Log
  5. import androidx.appcompat.app.AppCompatActivity
  6. import kotlinx.android.synthetic.main.activity_main.*
  7. import org.jivesoftware.smack.*
  8. import org.jivesoftware.smack.filter.StanzaTypeFilter.MESSAGE
  9. import org.jivesoftware.smack.packet.Message
  10. import org.jivesoftware.smack.packet.Stanza
  11. import org.jivesoftware.smack.tcp.XMPPTCPConnection
  12. import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration
  13. import org.jivesoftware.smack.util.TLSUtils
  14. class MainActivity : AppCompatActivity() {
  15. override fun onCreate(savedInstanceState: Bundle?) {
  16. super.onCreate(savedInstanceState)
  17. setContentView(R.layout.activity_main)
  18. SmackConfiguration.DEBUG = true;
  19. val config = XMPPTCPConnectionConfiguration.builder()
  20. config.setUsernameAndPassword( "2", "12345678")
  21. .setXmppDomain( "xxx.net")
  22. .setConnectTimeout( 30000)
  23. var mBuild =
  24. TLSUtils.acceptAllCertificates(
  25. TLSUtils.disableHostnameVerificationForTlsCertificates(
  26. config
  27. )
  28. ).build();
  29. val conn2: AbstractXMPPConnection = XMPPTCPConnection(mBuild)
  30. conn2.replyTimeout = 30000
  31. var runnable = object : Runnable {
  32. override fun run() {
  33. try {
  34. conn2.connect().login()
  35. } catch (e: Exception) {
  36. Log.e( "TAG", " conn2.connect()Exception= " + e.message)
  37. }
  38. }
  39. }
  40. Thread(runnable).start()
  41. // val chatManager: ChatManager = ChatManager.getInstanceFor(conn2)
  42. // chatManager.addIncomingListener(IncomingChatMessageListener { from, message, chat ->
  43. // chat.send(message.getBody());
  44. // Log.e("TAG", "New message from " + from + ": " + message.body)
  45. // })
  46. conn2.addAsyncStanzaListener( object : StanzaListener {
  47. override fun processStanza(packet: Stanza?) {
  48. //连接成功以后的数据回调
  49. val headlineMessage = packet as Message
  50. Log.e( "TAG", "headlineMessage======" + headlineMessage)
  51. }
  52. }, MESSAGE)
  53. conn2.addConnectionListener( object : ConnectionListener {
  54. override fun connected(connection: XMPPConnection?) {
  55. Log.e( "TAG", "connected()=== " + connection.toString())
  56. }
  57. override fun connectionClosed() {
  58. Log.e( "TAG", "connectionClosed()=== ")
  59. }
  60. override fun connectionClosedOnError(e: Exception?) {
  61. if (e != null) {
  62. Log.e( "TAG", "connectionClosedOnError()=== " + e.message)
  63. }
  64. }
  65. override fun authenticated(connection: XMPPConnection?, resumed: Boolean) {
  66. Log.e( "TAG", "authenticated()=== " + resumed)
  67. Log.e( "TAG", " mConnection.isAuthenticated();=======" + conn2.isAuthenticated())
  68. }
  69. })
  70. logins.setOnClickListener {
  71. var runnable = object : Runnable {
  72. override fun run() {
  73. Log.e( "TAG", "连接状态=" + conn2.isConnected)
  74. if (!conn2.isConnected) {
  75. try {
  76. conn2.connect()
  77. } catch (e: Exception) {
  78. Log.e( "TAG", " conn2.connect() Exception= " + e.message)
  79. }
  80. }
  81. }
  82. }
  83. Thread(runnable).start()
  84. }
  85. disLogin.setOnClickListener {
  86. var runnable = object : Runnable {
  87. override fun run() {
  88. conn2.disconnect()
  89. }
  90. }
  91. Thread(runnable).start()
  92. }
  93. }
  94. }

介绍:看文档

 


转载:https://blog.csdn.net/qq_36333309/article/details/111480184
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场