a179583637
a179583637
采纳率35.1%
2016-07-18 05:31 阅读 2.9k

QQ第三方登录,当点击腾讯的登录之后会返回到LoginActivity

返回LoginActivity之后必须得点击back键一下才会显示登录成功的对话框,这是问题之一,而且获取不到登录用户的信息,这是问题之二,怎么能在点击了腾讯登录按钮之后直接返回到MainActivity取值,而不是返回到LoginActivity。
第一张图,我的MainAcitvity
图片说明
第二张图,我的LoginActivity
图片说明
第三张图,腾讯的登录!
图片说明
第四,我的代码
public class LoginActivity extends Activity {
private static final String TAG = MainActivity.class.getName();
public static String mAppid;
public static QQAuth mQQAuth;
private UserInfo mInfo;
private Tencent mTencent;
private final String APP_ID = "222222";// 测试时使用,真正发布的时候要换成自己的APP_ID
String nickname;
Button okbutton;
ImageButton QQ_denglu;
EditText id;
EditText pass;
String uname;
LoadingDialog dialog;

HttpClient httpClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    final ActionBar actionBar = getActionBar();
    actionBar.hide();
    httpClient = new DefaultHttpClient();
    okbutton = (Button) findViewById(R.id.ok);
    QQ_denglu = (ImageButton) findViewById(R.id.QQ_denglu);
    QQ_denglu.setOnClickListener(qqdenglulistener);
    okbutton.setOnClickListener(loginokListener);
    id = (EditText) findViewById(R.id.login_id);
    pass = (EditText) findViewById(R.id.login_password);
    Button loginback = (Button) findViewById(R.id.backlogin);
    loginback.setOnClickListener(backloginListener);

}

View.OnClickListener backloginListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent in = getIntent();
        in.putExtra("result", "欢迎登录家友");
        LoginActivity.this.setResult(1, in);
        LoginActivity.this.finish();
    }
};
View.OnClickListener qqdenglulistener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onClickLogin();
    }
};
View.OnClickListener loginokListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //startActivity(new Intent(LoginActivity.this, DialogActivity.class));
        dialog = new LoadingDialog(LoginActivity.this);
        dialog.setTitle("登录中,请稍后...");
        // 执行输入校验
        if (validate())

        {
            // 如果登录成功
            if (loginPro()) {
                dialog.dismiss();
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG);
                Intent in = getIntent();
                in.putExtra("result", uname);
                LoginActivity.this.setResult(0, in);
                LoginActivity.this.finish();

            } else {
                dialog.dismiss();
                Toast.makeText(LoginActivity.this,
                        "用户名或密码错误!", Toast.LENGTH_LONG).show();
            }
        }


    }
};

private boolean loginPro() {
    // 获取用户输入的用户名、密码
    String uid = id.getText().toString();
    String upass = pass.getText().toString();
    try {
        uname = loginyanzheng(uid, upass);
        // 如果uname不为空
        if (!uname.equals("") && !uname.equals("null")) {
            return true;
        }
    } catch (Exception e) {
        Toast.makeText(this, "服务器响应异常,请稍后重试!", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

    return false;
}

// 对用户输入的用户名、密码进行校验
private boolean validate() {
    String uid = id.getText().toString().trim();
    if (uid.equals("")) {
        Toast.makeText(this, "账号不许为空!", Toast.LENGTH_LONG).show();
        return false;
    }
    String upass = pass.getText().toString().trim();
    if (upass.equals("")) {
        Toast.makeText(this, "请输入密码!", Toast.LENGTH_LONG).show();
        return false;
    }
    return true;
}

// 定义发送请求的方法
private String loginyanzheng(String id, String pass) throws Exception {
    // 使用Map封装请求参数
    Map<String, String> map = new HashMap<>();
    map.put("id", id);
    map.put("pass", pass);
    // 定义发送请求的URL
    String url = HttpUtil.BASE_URL + "LoginServlet";
    // 发送请求
    return HttpUtil.postRequest(url, map);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Intent in = getIntent();
        in.putExtra("result", "欢迎登录家友");
        LoginActivity.this.setResult(1, in);
        LoginActivity.this.finish();
    }
    return false;
}

//QQ-------------------------QQ-----------------------QQ-----------QQ----------------QQ

@Override
protected void onStart() {
    Log.d(TAG, "-->onStart");
    final Context context = LoginActivity.this;
    final Context ctxContext = context.getApplicationContext();
    mAppid = APP_ID;
    mQQAuth = QQAuth.createInstance(mAppid, ctxContext);
    mTencent = Tencent.createInstance(mAppid, LoginActivity.this);
    super.onStart();
}

private void updateUserInfo() {
    if (mQQAuth != null && mQQAuth.isSessionValid()) {
        IUiListener listener = new IUiListener() {

            @Override
            public void onError(UiError e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onComplete(final Object response) {
                Message msg = new Message();
                msg.obj = response;
                msg.what = 0;
                mHandler.sendMessage(msg);
            }

            @Override
            public void onCancel() {
            }
        };
        mInfo = new UserInfo(this, mQQAuth.getQQToken());
        mInfo.getUserInfo(listener);
    }
}

Handler mHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        if (msg.what == 0) {
            JSONObject response = (JSONObject) msg.obj;
            if (response.has("nickname")) {
                try {
                    nickname=response.getString("nickname");
                    Intent in = getIntent();
                    in.putExtra("result",nickname);
                    LoginActivity.this.setResult(0, in);
                    LoginActivity.this.finish();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }


};

private void onClickLogin() {
    if (!mQQAuth.isSessionValid()) {
        IUiListener listener = new BaseUiListener() {
            @Override
            protected void doComplete(JSONObject values) {
                updateUserInfo();
            }
        };
        mQQAuth.login(this, "all", listener);
        mTencent.login(this, "all", listener);
    } else {
        mQQAuth.logout(this);
        updateUserInfo();
    }
}
IUiListener loginListener = new BaseUiListener() {
    @Override
    protected void doComplete(JSONObject values) {
        Log.d("SDKQQAgentPref", "AuthorSwitch_SDK:" + SystemClock.elapsedRealtime());
        updateUserInfo();
    }
};

private class BaseUiListener implements IUiListener {

    @Override
    public void onComplete(Object response) {
        Util.showResultDialog(LoginActivity.this, response.toString(),
                "登录成功");
        doComplete((JSONObject) response);
    }

    protected void doComplete(JSONObject values) {

    }

    @Override
    public void onError(UiError e) {
        Util.toastMessage(LoginActivity.this, "onError: " + e.errorDetail);
        Util.dismissDialog();
    }

    @Override
    public void onCancel() {
        Util.toastMessage(LoginActivity.this, "onCancel: ");
        Util.dismissDialog();
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "-->onActivityResult " + requestCode  + " resultCode=" + resultCode);
    if (requestCode == Constants.REQUEST_LOGIN ||
            requestCode == Constants.REQUEST_APPBAR) {
        Tencent.onActivityResultData(requestCode,resultCode,data,loginListener);
    }

    super.onActivityResult(requestCode, resultCode, data);
}

@Override
protected void onResume() {
    Log.d(TAG, "-->onResume");
    super.onResume();
}


@Override
protected void onStop() {
    Log.d(TAG, "-->onStop");
    super.onStop();
}

@Override
protected void onDestroy() {
    Log.d(TAG, "-->onDestroy");
    super.onDestroy();
}
//返回动画
protected void onPause() {
    super.onPause();
    overridePendingTransition(R.anim.in_from_down,
            R.anim.out_to_up);
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • dongfeng9ge 东风玖哥 2016-07-19 08:55

    你没有设置QQ登录的登陆后界面呀

    点赞 评论 复制链接分享

相关推荐