public class LoginActivity extends Activity {
private TextView titletext;//标题头上面的文字
boolean isno=false;
private LinearLayout login_lay;//需要模糊的背景
private Bitmap bitmap;//模糊的图片
private ImageButton login_qq;//qq登录按钮
private static final String APPID = "1104887806";//测试用的开发代码,需要自己去申请
private Tencent mTencent; //qq主操作对象
private IUiListener loginListener; //授权登录监听器
private IUiListener userInfoListener; //获取用户信息监听器
private String scope; //获取信息的范围参数
private UserInfo userInfo; //qq用户信息
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login_layout);
login_qq=(ImageButton) findViewById(R.id.login_qq);
titletext=(TextView) findViewById(R.id.title_textview);
titletext.setText("登录");
login_lay=(LinearLayout) findViewById(R.id.login_lay);
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.agbc);
setupViews();
initData();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
if (hasFocus&!isno) {
BlurUtil blurUtil=new BlurUtil(getApplicationContext());
blurUtil.blur(bitmap, login_lay);
}
}
/**
* 注销函数
*/
@Override
protected void onDestroy() {
if (mTencent != null) {
//注销登录
//mTencent.logout(LoginActivity.this);
} super.onDestroy();
}
private void setupViews() {
login_qq.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
login();
}
});
}
private void initData() {
//初始化qq主操作对象
try {
mTencent = Tencent.createInstance(APPID, this.getApplicationContext());
} catch (Exception e) {
// TODO: handle exception
}
//要所有权限,不然会再次申请增量权限,这里不要设置成get_user_info,add_t
scope = "all";
loginListener = new IUiListener(){
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
/**
* 返回json数据样例
*
* {"ret":0,"pay_token":"D3D678728DC580FBCDE15722B72E7365",
* "pf":"desktop_m_qq-10000144-android-2002-",
* "query_authority_cost":448,
* "authority_cost":-136792089,
* "openid":"015A22DED93BD15E0E6B0DDB3E59DE2D",
* "expires_in":7776000,
* "pfkey":"6068ea1c4a716d4141bca0ddb3df1bb9",
* "msg":"",
* "access_token":"A2455F491478233529D0106D2CE6EB45",
* "login_cost":499}
*/
@Override
public void onComplete(Object value) {
System.out.println("有数据返回..");
if (value == null) {
return;
}
try {
JSONObject jo = (JSONObject) value;
int ret = jo.getInt("ret");
System.out.println("json=" + String.valueOf(jo));
if (ret == 0) {
Toast.makeText(LoginActivity.this, "登录成功",Toast.LENGTH_LONG).show();
String openID = jo.getString("openid");
String accessToken = jo.getString("access_token");
String expires = jo.getString("expires_in");
mTencent.setOpenId(openID);
mTencent.setAccessToken(accessToken, expires);
}
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onError(UiError arg0) {
// TODO Auto-generated method stub
}
};
userInfoListener = new IUiListener(){
/**
* 返回用户信息样例
*
json={"ret":0,"pay_token":"DE1E2CEE3C2D48C3BEBFD2AA6443809E","pf":"desktop_m_qq-10000144-android-2002-","query_authority_cost":396,"authority_cost":-377502841,"openid":"35075D6E4D0CD2E22DDB6C851899E962","expires_in":7776000,"pfkey":"52263cfaa53eef31877ec7e08fba4762","msg":"","access_token":"53B30EC68A90A4CDF62185C06E644AD0","login_cost":544} *
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
@Override
public void onComplete(Object arg0) {
// TODO Auto-generated method stub
if(arg0 == null){
return;
}
try {
JSONObject jo = (JSONObject) arg0;
int ret = jo.getInt("ret");
System.out.println("" + String.valueOf(jo));
String nickName = jo.getString("nickname");
String gender = jo.getString("gender");
Toast.makeText(LoginActivity.this, "你好," + nickName,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onError(UiError arg0) {
// TODO Auto-generated method stub
}
};
}
//登录函数
private void login() {
//如果session无效,就开始登录
if (!mTencent.isSessionValid()) {
//开始qq授权登录
mTencent.login(LoginActivity.this, scope, loginListener);
}
}
/**
* 在某些低端机上调用登录后,由于内存紧张导致APP被系统回收,登录成功后无法成功回传数据。
解决办法如下
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if (requestCode == Constants.REQUEST_API) {
// if (resultCode == Constants.REQUEST_LOGIN) {
// Tencent.handleResultData(data, loginListener);
// }
//
// }
Tencent.onActivityResultData(requestCode, resultCode, data,loginListener);
}
}