Thursday, December 11, 2014

Android XMPP delete user

To register in XMPP server first of all you need to CREATE XMPP CONNECTION

After connection to XMPP server. If you don't have registration in XMPP server, for Registration click here

For delete user from XMPP server you need to use AccountManager  class for get account and than deleteAccount() method is used for delete the account.

For delete account from XMPP server user must login to XMPP server. For login to XMPP server 

Below is example code for delete user from XMPP server.

XML file layout is below.

<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" >

    <Button
        android:id="@+id/btn_Delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_centerInParent="true"
        android:layout_marginTop="38dp"
        android:text="Delete User" />

</RelativeLayout>

Use below activity for the login to XMPP server.

import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
 
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class TestActivity extends Activity{
 
 private Button btn_Delete;
 
 XMPPConnection connection ;
 ConnectionConfiguration config;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.demo_activity);
  
  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
      .detectAll()
      .penaltyLog()
      .build();
  StrictMode.setThreadPolicy(policy);
 
  btn_Delete = (Button) findViewById(R.id.btn_Delete);
  
  btn_Delete.setOnClickListener(new OnClickListener() {
 
   @Override
   public void onClick(View v) {
    try {
     if (connection.isConnected()) {
      connection.disconnect();
      connection.connect();
      connection.login("USERNAME", "PASSWORD");
        AccountManager accountManager = connection.getAccountManager();
        accountManager.deleteAccount();
        connection.disconnect();
//        System.exit(0);
     }else {
      connection.connect();
       AccountManager accountManager = connection.getAccountManager();
        accountManager.deleteAccount();
        connection.disconnect();
//        System.exit(0);
     }
     
    } catch (XMPPException e) {
     e.printStackTrace();
    }
   }
  });
 }
}

Hope this post is helpful for you...

Android XMPP login

To register in XMPP server first of all you need to CREATE XMPP CONNECTION

After connection to XMPP server. If you don't have registration in XMPP server, for Registration click here

For login  login("USERNAME", "PASSWORD") method is used.  In this method you need to pass username and password as parameter.

For this example below is the code for xml file.

<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" >

    <Button
        android:id="@+id/btn_Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_centerInParent="true"
        android:layout_marginTop="38dp"
        android:text="Login" />

</RelativeLayout>


Use below activity for the login to XMPP server.

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
 
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class TestActivity extends Activity{
 
 private Button btn_Login;
 
 XMPPConnection connection ;
 ConnectionConfiguration config;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.demo_activity);
  
  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
      .detectAll()
      .penaltyLog()
      .build();
  StrictMode.setThreadPolicy(policy);
 
  
  btn_Login = (Button) findViewById(R.id.btn_Login);
  
  btn_Login.setOnClickListener(new OnClickListener() {
 
   @Override
   public void onClick(View v) {
    try {
     try {
      if (connection.isConnected()) {
       connection.disconnect();
       connection.connect();
       connection.login("USERNAME", "PASSWORD");
       
      }else {
       connection.connect();
       connection.login("USERNAME", "PASSWORD");
       
      }
      
     } catch (XMPPException e) {
      e.printStackTrace();
     }
     
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }
}

Hope this post may helpful for you...

Friday, December 5, 2014

Android XMPP registration

To register in XMPP server first of all you need to connect with XMPP server.

For register new user in XMPP server AccountManager class is used

To get account manager below code snippet is used
 
AccountManager accountManager = connection.getAccountManager();

For register new user in XMPP server below method is used.

createAccount("USERNAME", "PASSWORD")

For this example below is the code for xml file.

<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" >

    <Button
        android:id="@+id/btn_Register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_centerInParent="true"
        android:layout_marginTop="38dp"
        android:text="Register" />

</RelativeLayout>

Use below activity for the register new user to XMPP server.

public class DemoActivity extends Activity{
 
 private Button btn_Register;
 XMPPConnection connection ;
 ConnectionConfiguration config;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.demo_activity);
 
  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
       .detectAll()
       .penaltyLog()
       .build();
   StrictMode.setThreadPolicy(policy);

  new ConnectToXmpp().execute();
  
  btn_Register = (Button) findViewById(R.id.btn_Register);
  btn_Register.setOnClickListener(new OnClickListener() {
 
   @Override
   public void onClick(View v) {
     try {
       AccountManager accountManager = connection.getAccountManager();
               accountManager.createAccount("USERNAME", "PASSWORD");
    } catch (XMPPException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
            
   }
  });
 }
 private class ConnectToXmpp extends AsyncTask<Void, Void, Void> {
 
     @Override
     protected Void doInBackground(Void... params) {
           config = new ConnectionConfiguration("YOUR SERVER IP", PORT);
           connection = new XMPPConnection(config);
     try {
          connection.connect();
          connection.login("ADMIN LOGIN", "ADMIN PASSWORD");
     } catch (XMPPException e) {
         e.printStackTrace();
     } 
 
         return null;
     }
 
     @Override
     protected void onPostExecute(Void result) {
 
     }
 
 }
}


Hope this post may helpful for you....

Android create XMPPConnection

To create XMPP connection in android you required smack.jar file in your project.

Download smack.jar file from Here

Add jar file in lib folder of your project and than add to build path.

To create connection with XMPP server first of all create XMPPConnection.
Use below code snippet for connection with server.

 
ConnectionConfiguration  config = new ConnectionConfiguration(“YOUR SERVER IP, PORT);
XMPPConnection  connection = new XMPPConnection(config);


To create XMPP connection in your android put this below code in your  activity for XMPP connection


 
public class MainActivity extends Activity {
 
 XMPPConnection connection ;
 ConnectionConfiguration config;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  new ConnectToXmpp().execute();
  
 }
 private class ConnectToXmpp extends AsyncTask {
 
     @Override
     protected Void doInBackground(Void... params) {
           config = new ConnectionConfiguration(“YOUR SERVER IP”, PORT);
           connection = new XMPPConnection(config);
     try {
          connection.connect();
          connection.login("ADMIN USERNAME”, “ADMIN PASSWORD”);
     } catch (XMPPException e) {
         e.printStackTrace();
     } 
 
         return null;
     }
 
     @Override
     protected void onPostExecute(Void result) {
 
     }
 
 }
}

That's it...

Hope this post may helpful for you...