1. 程式人生 > >環信sdk指定聊天列子

環信sdk指定聊天列子

public class Login extends Activity implements Button.OnClickListener {


    EditText account, password;
    Button login;
    handle hn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);


        account = findViewById(R.id.account);
        password = findViewById(R.id.password);
        login = findViewById(R.id.button);
        login.setOnClickListener(this);
        hn = new handle();


        EMOptions options = new EMOptions();
// 預設新增好友時,是不需要驗證的,改成需要驗證
        options.setAcceptInvitationAlways(false);
// 是否自動將訊息附件上傳到環信伺服器,預設為True是使用環信伺服器上傳下載,如果設為 false,需要開發者自己處理附件訊息的上傳和下載
        options.setAutoTransferMessageAttachments(false);
// 是否自動下載附件類訊息的縮圖等,預設為 true 這裡和上邊這個引數相關聯
        options.setAutoDownloadThumbnail(false);
//初始化
        EMClient.getInstance().init(getApplicationContext(), options);
//在做打包混淆時,關閉debug模式,避免消耗不必要的資源
        EMClient.getInstance().setDebugMode(true);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                Log.d("main", "1");
                EMClient.getInstance().login(account.getText().toString(), password.getText().toString(), new EMCallBack() {//回撥
                    @Override
                    public void onSuccess() {
                        EMClient.getInstance().groupManager().loadAllGroups();
                        EMClient.getInstance().chatManager().loadAllConversations();
                        Log.d("main", "登入聊天伺服器成功!");


                        Message message = new Message();
                        Bundle bundle = new Bundle();
                        bundle.putString("1", "登入聊天伺服器成功");
                        message.setData(bundle);

                        Login.this.hn.sendMessage(message);


                    }

                    @Override
                    public void onProgress(int progress, String status) {

                    }

                    @Override
                    public void onError(int code, String message) {
                        Log.d("main", "登入聊天伺服器失敗!");
                        Message message2 = new Message();
                        Bundle bundle = new Bundle();
                        bundle.putString("1", "登入聊天伺服器失敗");
                        message2.setData(bundle);

                        Login.this.hn.sendMessage(message2);

                    }
                });
                break;
        }
    }

    class handle extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            Bundle bundle = msg.getData();
            Toast.makeText(getApplicationContext(), bundle.getString("1"), Toast.LENGTH_SHORT).show();

        }
    }
}