@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.demo_login); loginHelper = LoginSampleHelper.getInstance(); userIdView = (EditText) findViewById(R.id.account); passwordView = (EditText) findViewById(R.id.password); appKeyView = (EditText) findViewById(R.id.appkey); progressBar = (ProgressBar) findViewById(R.id.login_progress); // 读取本地保存的账号信息 String localId = IMPrefsTools.getStringPrefs(LoginActivity.this, USER_ID, ""); if (!TextUtils.isEmpty(localId)) { userIdView.setText(localId); String localPassword = IMPrefsTools.getStringPrefs(LoginActivity.this, PASSWORD, ""); if (!TextUtils.isEmpty(localPassword)) { passwordView.setText(localPassword); } } // 不需要生成随机账号 // 也不需要生成随机账号的密码 // appKey就更用不到了有自己的appkey // 初始化账号信息 init(userIdView.getText().toString()); myRegisterReceiver(); loginButton = (Button) findViewById(R.id.login); Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.splash_middle_pic); lg = (ImageView) findViewById(R.id.logo); lg.setImageBitmap(logo); userIdView.addTextChangedListener( new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (TextUtils.isEmpty(s)) { passwordView.setText(""); } } @Override public void afterTextChanged(Editable s) {} }); loginButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // 判断当前网络状态,若当前无网络则提示用户无网络 if (YWChannel.getInstance().getNetWorkState().isNetWorkNull()) { Toast.makeText(LoginActivity.this, "网络已断开,请稍后再试哦~", Toast.LENGTH_SHORT).show(); return; } loginButton.setClickable(false); final Editable userId = userIdView.getText(); final Editable password = passwordView.getText(); if (userId == null || userId.toString().length() == 0) { Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show(); loginButton.setClickable(true); return; } if (password == null || password.toString().length() == 0) { Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show(); loginButton.setClickable(true); return; } InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(userIdView.getWindowToken(), 0); imm.hideSoftInputFromWindow(passwordView.getWindowToken(), 0); init(userId.toString()); progressBar.setVisibility(View.VISIBLE); loginHelper.login_Sample( userId.toString(), password.toString(), LoginSampleHelper.APP_KEY, new IWxCallback() { @Override public void onSuccess(Object... arg0) { saveIdPasswordToLocal(userId.toString(), password.toString()); loginButton.setClickable(true); progressBar.setVisibility(View.GONE); Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); YWLog.i(TAG, "login success!"); // FIXME: 2015/12/16 登录成功跳转到哪里去 // Intent intent = new Intent(LoginActivity.this, // FragmentTabs.class); // intent.putExtra(FragmentTabs.LOGIN_SUCCESS, // "loginSuccess"); // LoginActivity.this.startActivity(intent); // LoginActivity.this.finish(); // YWIMKit mKit = LoginSampleHelper.getInstance().getIMKit(); // EServiceContact contact = new EServiceContact("qn店铺测试账号001:找鱼"); // // LoginActivity.this.startActivity(mKit.getChattingActivityIntent(contact)); // mockConversationForDemo(); // 登录成功了可以发送一个消息试试 IYWConversationService conversationService = LoginSampleHelper.getInstance().getIMKit().getConversationService(); YWConversation conversation = conversationService .getConversationCreater() .createConversationIfNotExist("123"); YWMessage msg = YWMessageChannel.createTextMessage("hello"); if (conversation.getLastestMessage() == null) { conversation.getMessageSender().sendMessage(msg, 120, null); } } @Override public void onProgress(int arg0) { // TODO Auto-generated method stub } @Override public void onError(int errorCode, String errorMessage) { progressBar.setVisibility(View.GONE); if (errorCode == YWLoginCode.LOGON_FAIL_INVALIDUSER) { // 若用户不存在,则提示使用游客方式登陆 showDialog(GUEST); } else { loginButton.setClickable(true); YWLog.w(TAG, "登录失败,错误码:" + errorCode + " 错误信息:" + errorMessage); ToastUtil.showToast("登录失败,错误码:" + errorCode + " 错误信息:" + errorMessage); } } }); } }); }
/** * 保存登录的用户名密码到本地 * * @param userId * @param password */ private void saveIdPasswordToLocal(String userId, String password) { IMPrefsTools.setStringPrefs(LoginActivity.this, USER_ID, userId); IMPrefsTools.setStringPrefs(LoginActivity.this, PASSWORD, password); }