[置顶] Android基于XMPP Smack及Openfire学习笔记(2)

接着Android基于XMPP Smack及Openfire学习笔记(1)实现帐号登录及修改密码:

//这里MainActivity中的Onclick方法
@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login://登录 
                startActivity(new Intent(MainActivity.this, LoginActivity.class));
                break;
            case R.id.update_pwd://修改密码
                startActivity(new Intent(MainActivity.this, ChangePwdActivity.class));
                break;
        }
    }
//登录实现,布局是用的上一篇文章中注册页面布局
/** * Openfire登录 * @description: * @date 2016-2-20 下午1:35:58 */
public class LoginActivity extends ActionBarActivity implements OnClickListener {
    private Button login;
    private EditText edt_name;
    private EditText edt_pwd;
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            Toast.makeText(LoginActivity.this, msg.what == 1 ? "登录成功" : "登录失败", Toast.LENGTH_SHORT).show();
            finish();// 一般我们登录成功后会跳转到指定页面
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        initViewsAndEvents();
    }

    private void initViewsAndEvents() {
        this.login = (Button) findViewById(R.id.register);
        login.setText("登录");
        this.login.setOnClickListener(this);
        this.edt_name = (EditText) findViewById(R.id.edt_name);
        this.edt_pwd = (EditText) findViewById(R.id.edt_pwd);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.register) {
            if (TextUtils.isEmpty(edt_name.getText().toString().trim())) {
                Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(edt_pwd.getText().toString().trim())) {
                Toast.makeText(this, "请输入用密码", Toast.LENGTH_SHORT).show();
                return;
            }
            new Thread(new Runnable() {

                @Override
                public void run() {
                    boolean success = loginToServier(edt_name.getText().toString().trim(), edt_pwd.getText().toString().trim());
                    Message msg = new Message();
                    msg.what = success ? 1 : 0;
                    mHandler.sendMessage(msg);
                    Log.e("ldm", "是否成功" + success);
                }
            }).start();
        }
    }

    /** * 登录到服务器 * @description: * @date 2016-2-20 下午2:23:15 */
    private boolean loginToServier(String name, String pwd) {
        boolean loginSuccess = false;
        // 初始化ConnectionConfiguration,参数是服务器ip,端口号及服务器名称
        ConnectionConfiguration config = new ConnectionConfiguration("192.168.56.1", 5222, "hx1401016");
        // 设置是否启用安全验证
        config.setSelfSignedCertificateEnabled(false);
        // 设置启用调试
        config.setDebuggerEnabled(true);
        // 允许自动连接
        config.setReconnectionAllowed(true);
        config.setSendPresence(true);
        // 设置安全模式
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        // 初始连接对象XMPPConnection
        XMPPConnection connection = new XMPPConnection(config);
        try {
            connection.connect();
            connection.login(name, pwd);
            loginSuccess = true;
        }
        catch (XMPPException e) {
            e.printStackTrace();
        }
        return loginSuccess;
    }
}
//修改密码,布局中就一个EditText和Button
public class ChangePwdActivity extends ActionBarActivity implements OnClickListener {
    private Button change;
    private EditText new_pwd;
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            Toast.makeText(ChangePwdActivity.this, msg.what == 1 ? "修改成功" : "修改失败", Toast.LENGTH_SHORT).show();
            finish();// 一般我们登录成功后会跳转到指定页面
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_pwd);
        initViewsAndEvents();
    }

    private void initViewsAndEvents() {
        this.change = (Button) findViewById(R.id.change);
        this.change.setOnClickListener(this);
        this.new_pwd = (EditText) findViewById(R.id.new_pwd);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.change) {
            if (!isLegal()) return;
            new Thread(new Runnable() {// android4.0以后不能在主线程发起网络请求不能在主线程中进行网络请求
                        @Override
                        public void run() {
                            boolean success = changePwd(new_pwd.getText().toString().trim());
                            Message msg = new Message();
                            msg.what = success ? 1 : 0;
                            mHandler.sendMessage(msg);
                        }
                    }).start();
        }
    }

    /** * 判断输入的用户名及密码 * @description: * @date 2016-2-20 下午2:00:24 */
    private boolean isLegal() {
        boolean isOk = true;
        if (TextUtils.isEmpty(new_pwd.getText().toString())) {
            Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
            isOk = false;
        }
        return isOk;

    }

    /** * 修改密码:只需要传入新密码即可 * @description: * @date 2016-2-20 下午2:07:44 */
    private boolean changePwd(String pwd) {
        boolean loginSuccess = false;
        // 初始化ConnectionConfiguration,参数是服务器ip,端口号及服务器名称
        ConnectionConfiguration config = new ConnectionConfiguration("192.168.56.1", 5222, "hx1401016");
        // 设置是否启用安全验证
        config.setSelfSignedCertificateEnabled(false);
        // 设置启用调试
        config.setDebuggerEnabled(true);
        // 允许自动连接
        config.setReconnectionAllowed(true);
        config.setSendPresence(true);
        // 设置安全模式
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        // 初始连接对象XMPPConnection
        XMPPConnection connection = new XMPPConnection(config);
        try {
            connection.connect();// 连接服务器
            // 修改密码的前提是要处理登录状态
            // 我这里先要点击登录按钮,再点击修改密码,实际项目中逻辑更复杂
            connection.getAccountManager().changePassword(pwd);//如果修改成功, 可以用用新密码尝试登录进行验证
            loginSuccess = true;
        }
        catch (XMPPException e) {
            e.printStackTrace();
        }
        return loginSuccess;
    }

}

上一篇文章忘记提醒大家,我们所有的操作都是要有在网络的情况下进行,所以记得添加网络权限。

你可能感兴趣的:([置顶] Android基于XMPP Smack及Openfire学习笔记(2))