Guest User

Untitled

a guest
Oct 16th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. package com.spario.jabber_test_library;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. import com.spario.xmpplogintest.events.connectionListener.ConnectionListener;
  11. import com.spario.xmpplogintest.events.connectionListener.OnConnectionListener;
  12. import com.spario.xmpplogintest.networkinfo.ConnectivityReceiver;
  13. import com.spario.xmpplogintest.networkinfo.SparioApplication;
  14. import com.spario.xmpplogintest.xmpp.DataReceiver;
  15. import com.spario.xmpplogintest.xmpp.OnDataReciever;
  16. import com.spario.xmpplogintest.xmppConnection;
  17.  
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20. TextView tid,tpass;
  21. Button blogin,blogout;
  22. xmppConnection xmpp;
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27. tid=(TextView)findViewById(R.id.txtid);
  28. tpass=(TextView)findViewById(R.id.txtpass);
  29. blogin=(Button) findViewById(R.id.btnlogin);
  30. blogout=(Button) findViewById(R.id.btnlogout);
  31. xmpp = new xmppConnection();
  32. ///button widget event onClick //for login
  33. blogin.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. //set id and password
  37. xmpp.USER=tid.getText().toString();
  38. xmpp.PASSWORD=tpass.getText().toString();
  39. //set default presence autometically after login
  40. xmpp.setPresence(true);
  41. //xmpp.getRoster(true);
  42. xmpp.keepalive(true);
  43. xmpp.login();
  44. }
  45. });
  46. /////button widget event onClick //for logout
  47. blogout.setOnClickListener(new View.OnClickListener() {
  48. @Override
  49. public void onClick(View v) {
  50. xmpp.logOut();
  51. }
  52. });
  53.  
  54. //create connection listener callback methods
  55. ConnectionListener.setOnConnectionListener(new OnConnectionListener() {
  56. @Override
  57. public void onSocketConnected() {
  58. setToast("socketconnected");
  59. }
  60.  
  61. @Override
  62. public void onError(String s) {
  63. setToast(s);
  64. }
  65.  
  66. @Override
  67. public void onAuthenticated() {
  68. setToast("authenticated");
  69. }
  70.  
  71. @Override
  72. public void onAuthenticationFailed() {
  73. setToast("authenticationfailed");
  74. }
  75.  
  76. @Override
  77. public void onBlockoverIP() {
  78. setToast("idblocked over IP");
  79. }
  80.  
  81. @Override
  82. public void onConnected() {
  83. setToast("connected");
  84. }
  85.  
  86. @Override
  87. public void onDisconnected() {
  88. setToast("disconnected");
  89. }
  90.  
  91. });
  92. //this callback method gets input and output xml data...........
  93. DataReceiver.setOnDataReceiver(new OnDataReciever() {
  94. @Override
  95. public void onXmlReceive(String xml) {
  96. Log.i("spario_in",xml);
  97. }
  98.  
  99. @Override
  100. public void onXmlSend(String xml) {
  101. Log.i("spario_out",xml);
  102.  
  103. }
  104. });
  105. //the callback method inform network connection
  106. SparioApplication.setOnConnectivity(new ConnectivityReceiver.onConnectivityReceiver() {
  107. @Override
  108. public void onNetworkConnectionChanged(boolean b) {
  109. if(!b) {
  110. setToast("network gone");
  111. }else{
  112. setToast("network appear");
  113. }
  114. }
  115. });
  116. }
  117.  
  118. //use runOnUiThread to create Toast on events
  119. private void setToast(final String msg){
  120. runOnUiThread(new Runnable() {
  121. @Override
  122. public void run() {
  123. Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
  124. }
  125. });
  126. }
  127. }
Add Comment
Please, Sign In to add comment