QQ登陆及分享

//下载sdk 将jar包放入libs文件夹下 并添加到依赖库

//配置清单文件

android:name="com.tencent.tauth.AuthActivity"

android:noHistory="true"

android:launchMode="singleTask">

android:theme="@android:style/Theme.Translucent.NoTitleBar"

android:configChanges="orientation|keyboardHidden|screenSize"

/>

//添加依赖

compile files('libs/mta-sdk-1.6.2.jar')

compile files('libs/open_sdk_r5756.jar')

//声明用到的变量

private static finalStringTAG="MainActivity";

private staticTencentmTencent;

privateButtonbtn_login;

private static finalStringAPP_ID="1105864048";

private static booleanisServerSideLogin=false;

privateIUiListenerloginListener;

//QQ登陆的用户信息

privateUserInfomInfo;

privateTextViewtv_name;

privateImageViewiv_user;

privateButtonbtn_share;

privateButtonbtn_shareImage;

privateStringlocalUrl;

//初始化 Tencent

mTencent= Tencent.createInstance(APP_ID, this.getApplicationContext());

//登陆部分

//用户登陆后更新信息到ui界面

HandlermHandler=newHandler() {

@Override

public voidhandleMessage(Message msg) {

Log.d(TAG,"handleMessage: "+msg.what+"  !");

if(msg.what==0) {

JSONObject response = (JSONObject) msg.obj;

if(response.has("nickname")) {

try{

tv_name.setText(response.getString("nickname"));

}catch(JSONException e) {

e.printStackTrace();

}

}

}if(msg.what==1) {

Log.d(TAG,"handleMessage: "+msg.obj.toString()+"!!");

String s = msg.obj.toString();

x.image().bind(iv_user,s);

}

}

};

//获取QQ登陆后的信息

private voidupdateUserInfo() {

if(mTencent!=null&&mTencent.isSessionValid()) {

IUiListener listener =newIUiListener() {

@Override

public voidonError(UiError e) {

}

@Override

public voidonComplete(finalObject response) {

Message msg =newMessage();

msg.obj= response;

msg.what=0;

mHandler.sendMessage(msg);

newThread() {

@Override

public voidrun() {

JSONObject json = (JSONObject)response;

Log.d(TAG,"run: "+response.toString());

if(json.has("figureurl")) {

try{

String qq = json.getString("figureurl_qq_1");

Message msg =newMessage();

msg.obj=qq;

msg.what=1;

mHandler.sendMessage(msg);

}catch(JSONException e) {

e.printStackTrace();

}

}

}

}.start();

}

@Override

public voidonCancel() {

}

};

mInfo=newUserInfo(MainActivity.this,mTencent.getQQToken());

mInfo.getUserInfo(listener);

}else{

}

}

//重写点击事件的方法

//OnClick点击事件

@RequiresApi(api= Build.VERSION_CODES.LOLLIPOP_MR1)

@Override

public voidonClick(View view) {

switch(view.getId()) {

caseR.id.btn_login:

Toast.makeText(MainActivity.this,"点击登陆",Toast.LENGTH_SHORT).show();

doLogin();

Log.d(TAG,"onClick: "+ (mTencent.isSessionValid() ?"已登录":"未登录"));

break;

caseR.id.btn_share:

onClickShare();

break;

caseR.id.btn_shareImage:

shareImage(MainActivity.this);

break;

}

}

//登陆的方法

private voiddoLogin() {

loginListener=newBaseUiListener() {

@Override

protected voiddoComplete(JSONObject values) {

Log.d("SDKQQAgentPref","AuthorSwitch_SDK:"+ SystemClock.elapsedRealtime());

initOpenidAndToken(values);

}

};

if(!mTencent.isSessionValid()){

mTencent.login(MainActivity.this,"all",loginListener);

updateUserInfo();

}

}

//初始化openId 和token

public static voidinitOpenidAndToken(JSONObject jsonObject) {

try{

String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN);

String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN);

String openId = jsonObject.getString(Constants.PARAM_OPEN_ID);

if(!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires)

&& !TextUtils.isEmpty(openId)) {

mTencent.setAccessToken(token,expires);

mTencent.setOpenId(openId);

}

}catch(Exception e) {

}

}

//分享图片的方法(图文)

private voidonClickShare(){

Bundle parm=newBundle();

parm.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,QQShare.SHARE_TO_QQ_TYPE_DEFAULT);

parm.putString(QQShare.SHARE_TO_QQ_TITLE,"分享的标题");

parm.putString(QQShare.SHARE_TO_QQ_SUMMARY,"分享的摘要");

//点击后跳转的url

parm.putString(QQShare.SHARE_TO_QQ_TARGET_URL,"http://p0.so.qhmsg.com/dm/420_207_/t013675fef4614592c0.jpg");

//分享的网络图片地址1

parm.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"http://p0.so.qhmsg.com/dm/420_207_/t013675fef4614592c0.jpg");

mTencent.shareToQQ(MainActivity.this,parm, newIUiListener() {

@Override

public voidonCancel() {

}

@Override

public voidonComplete(Object response) {

//TODO Auto-generated method stub

Util.toastMessage(MainActivity.this,"onComplete: "+ response.toString());

}

@Override

public voidonError(UiError e) {

//TODO Auto-generated method stub

Util.toastMessage(MainActivity.this,"onError: "+ e.errorMessage,"e");

}

});

}

//分享本地图片的方法

@RequiresApi(api= Build.VERSION_CODES.LOLLIPOP_MR1)

private voidshareImage(Activity activity){

Intent intent =newIntent(Intent.ACTION_GET_CONTENT);

if(android.os.Build.VERSION.SDK_INT>= Util.Build_VERSION_KITKAT) {

intent.setAction(Util.ACTION_OPEN_DOCUMENT);

}else{

intent.setAction(Intent.ACTION_GET_CONTENT);

}

intent.addCategory(Intent.CATEGORY_OPENABLE);

intent.setType("image/*");

activity.startActivityForResult(

Intent.createChooser(intent,"本地图片"),0);

Bundle b=newBundle();

b.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,QQShare.SHARE_TO_QQ_TYPE_IMAGE);

b.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL,localUrl);

mTencent.shareToQQ(MainActivity.this,b,newBaseUiListener());

}

//回调监听

@Override

protected voidonActivityResult(intrequestCode, intresultCode,Intent data) {

Log.d(TAG,"-->onActivityResult "+ requestCode +" resultCode="+ resultCode);

if(requestCode == Constants.REQUEST_LOGIN||

requestCode == Constants.REQUEST_APPBAR) {

Tencent.onActivityResultData(requestCode,resultCode,data,loginListener);

updateUserInfo();

}

//获取本地要分享的图片的路径

if(requestCode == Constants.REQUEST_QQ_SHARE) {

Tencent.onActivityResultData(requestCode,resultCode,data,newBaseUiListener());

}else if(requestCode ==0) {

if(resultCode == Activity.RESULT_OK) {

if(data !=null&& data.getData() !=null) {

// 根据返回的URI获取对应的SQLite信息

Uri uri = data.getData();

localUrl= Util.getPath(this,uri);

}

}

}

super.onActivityResult(requestCode,resultCode,data);

}

//基本的IUilistener类

private classBaseUiListenerimplementsIUiListener {

@Override

public voidonComplete(Object response) {

if(null== response) {

Util.showResultDialog(MainActivity.this,"返回为空","登录失败");

return;

}

JSONObject jsonResponse = (JSONObject) response;

if(null!= jsonResponse && jsonResponse.length() ==0) {

Util.showResultDialog(MainActivity.this,"返回为空","登录失败");

return;

}

Util.showResultDialog(MainActivity.this,response.toString(),"登录成功!!!!!!!!!!");

// 有奖分享处理(未接入该功能)

Log.d(TAG,"onComplete: "+ response.toString());

doComplete((JSONObject) response);

}

protected voiddoComplete(JSONObject values) {

initOpenidAndToken(values);

}

@Override

public voidonError(UiError e) {

Util.toastMessage(MainActivity.this,"onError: "+ e.errorDetail);

Util.dismissDialog();

}

@Override

public voidonCancel() {

Util.toastMessage(MainActivity.this,"onCancel: ");

Util.dismissDialog();

if(isServerSideLogin) {

isServerSideLogin=false;

}

}

}

你可能感兴趣的:(QQ登陆及分享)