public class MainActivity extends Activity {
private UMShareAPI mShareAPI;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShareAPI = UMShareAPI.get(this);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mShareAPI.onActivityResult(requestCode, resultCode, data);
}
public void QQ(View v) {
SHARE_MEDIA platform = SHARE_MEDIA.QQ;
mShareAPI.doOauthVerify(this, platform, umAuthListener);
}
private UMAuthListener umAuthListener = new UMAuthListener() {
//成功
public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
if (action == UMAuthListener.ACTION_GET_PROFILE) {
//获取用户信息
Log.e("","data=" + data);
//头像
String avatar = data.get("profile_image_url");
//昵称
String nickName = data.get("screen_name");
//省份
String province = data.get("province");
//性别
String sex = data.get("gender");
} else if (action == UMAuthListener.ACTION_AUTHORIZE) {
// 授权登录成功
mShareAPI.getPlatformInfo(MainActivity.this, platform, umAuthListener);
Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();
}
}
public void onError(SHARE_MEDIA platform, int action, Throwable t) {
Toast.makeText(getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
}
public void onCancel(SHARE_MEDIA platform, int action) {
Toast.makeText(getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
}
};
}