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