QQ分享於登入
阿新 • • 發佈:2019-01-23
//登入與註冊在Activity中的JAVA程式碼
private void initView() { tv_name= (TextView) findViewById(R.id.name); tv_content= (TextView) findViewById(R.id.content); imageView= (ImageView) findViewById(R.id.user_logo); findViewById(R.id.new_login_btn).setOnClickListener(this); findViewById(R.id.new_login_close).setOnClickListener(this); findViewById(R.id.new_login_shareqq).setOnClickListener(this); findViewById(R.id.new_login_shareqzone).setOnClickListener(this); if (mTencent == null) { mTencent = Tencent.createInstance(mAppid, this); } } @Override public void onClick(View v) { switch (v.getId()){ case R.id.new_login_btn: onClickLogin();break; case R.id.new_login_close: mTencent.logout(MainActivity.this);//登出登入 break; case R.id.new_login_shareqq: onClickShare(); break; case R.id.new_login_shareqzone: shareToQQzone(); break; } } /** * 繼承的到BaseUiListener得到doComplete()的方法資訊*/ IUiListener loginListener = new BaseUiListener() { @Override protected void doComplete(JSONObject values) {//得到使用者的ID 和簽名等資訊 用來得到使用者資訊 Log.i("lkei",values.toString()); initOpenidAndToken(values); updateUserInfo(); } }; /*** * QQ平臺返回返回資料個體 loginListener的values */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == Constants.REQUEST_LOGIN || requestCode == Constants.REQUEST_APPBAR) { Tencent.onActivityResultData(requestCode,resultCode,data,loginListener); } super.onActivityResult(requestCode, resultCode, data); } private class BaseUiListener implements IUiListener { @Override public void onComplete(Object response) { if (null == response) { Toast.makeText(MainActivity.this, "登入失敗",Toast.LENGTH_LONG).show(); return; } JSONObject jsonResponse = (JSONObject) response; if (null != jsonResponse && jsonResponse.length() == 0) { Toast.makeText(MainActivity.this, "登入失敗",Toast.LENGTH_LONG).show(); return; } Toast.makeText(MainActivity.this, "登入成功",Toast.LENGTH_LONG).show(); doComplete((JSONObject)response); } protected void doComplete(JSONObject values) { } @Override public void onError(UiError e) { //登入錯誤 } @Override public void onCancel() { // 執行完成 } } /** * 獲取登入QQ騰訊平臺的許可權資訊(用於訪問QQ使用者資訊) * @param jsonObject */ public static void initOpenidAndToken(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 void onClickLogin() { if (!mTencent.isSessionValid()) { mTencent.login(this, "all", loginListener); } } private void updateUserInfo() { if (mTencent != null && mTencent.isSessionValid()) { IUiListener listener = new IUiListener() { @Override public void onError(UiError e) { } @Override public void onComplete(final Object response) { Message msg = new Message(); msg.obj = response; Log.i("tag", response.toString()); msg.what = 0; mHandler.sendMessage(msg); } @Override public void onCancel() { } }; mInfo = new UserInfo(this, mTencent.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 { Gson gson=new Gson(); User user=gson.fromJson(response.toString(),User.class); if (user!=null) { tv_name.setText("暱稱:"+user.getNickname()+" 性別:"+user.getGender()+" 地址:"+user.getProvince()+user.getCity()); tv_content.setText("頭像路徑:"+user.getFigureurl_qq_2()); Picasso.with(MainActivity.this).load(response.getString("figureurl_qq_2")).into(imageView); } } catch (JSONException e) { e.printStackTrace(); } } } } }; //qq分享 private void onClickShare() { final Bundle params = new Bundle(); params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT); params.putString(QQShare.SHARE_TO_QQ_TITLE, "要分享的標題"); params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "要分享的摘要"); params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "http://blog.csdn.net/DickyQie/article/list/1"); params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "http://imgcache.qq.com/qzone/space_item/pre/0/66768.gif"); params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "切切歆語"); params.putString(QQShare.SHARE_TO_QQ_EXT_INT, "其他附加功能"); mTencent.shareToQQ(MainActivity.this, params, new BaseUiListener1()); } //回撥介面 (成功和失敗的相關操作) private class BaseUiListener1 implements IUiListener { @Override public void onComplete(Object response) { doComplete(response); } protected void doComplete(Object values) { } @Override public void onError(UiError e) { } @Override public void onCancel() { } } @SuppressWarnings("unused") private void shareToQQzone() { try { final Bundle params = new Bundle(); params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT); params.putString(QzoneShare.SHARE_TO_QQ_TITLE, "切切歆語"); params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "sss"); params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, "http://blog.csdn.net/DickyQie/article/list/1"); ArrayList<String> imageUrls = new ArrayList<String>(); imageUrls.add("http://media-cdn.tripadvisor.com/media/photo-s/01/3e/05/40/the-sandbar-that-links.jpg"); params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls); params.putInt(QzoneShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN); Tencent mTencent = Tencent.createInstance("1106062414", MainActivity.this); mTencent.shareToQzone(MainActivity.this, params, new BaseUiListener1()); } catch (Exception e) { } }
//許可權
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- SDK2.1新增獲取使用者位置資訊 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_TASKS"/>
//配置
android:label="@string/app_name"
<activity android:name="com.tencent.open.yyb.AppbarActivity" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> <activity android:name="com.tencent.connect.avatar.ImageActivity" /> <activity android:name="com.tencent.connect.common.AssistActivity" android:configChanges="orientation|keyboardHidden" android:screenOrientation="behind" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> <activity android:name="com.tencent.tauth.AuthActivity" android:launchMode="singleTask" android:noHistory="true" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="tencent1106062414" /> <!--應用的AppId要相同--> </intent-filter> </activity>