目前大多數APP都包含了第三方平臺的登陸,特別是QQ和微信,這篇博客主要講的是如何實現QQ第三方平臺實現QQ登陸和分享功能,功能包含:android
先看看效果圖:微信
要實現以上功能,首先得去騰訊開放平臺 註冊成爲開發者,而後建立應用,獲得 APPID,以下圖:app
名稱就是APP名稱,可上傳Logo圖片,受權登陸時會顯示受權給那個APP,效果如圖:ide
建立成功後,取得APP ID便可,完成以上功能了。this
添加這兩個jar包spa
public static Tencent mTencent; public static String mAppid="申請的APPID"; if (mTencent == null) { mTencent = Tencent.createInstance(mAppid, this); }
/** * 繼承的到BaseUiListener獲得doComplete()的方法信息 */ IUiListener loginListener = new BaseUiListener() { @Override protected void doComplete(JSONObject values) {//獲得用戶的ID 和簽名等信息 用來獲得用戶信息 Log.i("lkei",values.toString()); initOpenidAndToken(values); updateUserInfo(); } };
//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 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("申請的APPID", MainActivity.this); mTencent.shareToQzone(MainActivity.this, params, new BaseUiListener1()); } catch (Exception e) { } }
在AndroidManifest.xml中.net
application 下:code
<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>
添加權限cdn
<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"/>
源碼有點多就不一一貼出來了,直接下載源碼便可,其餘功能能夠參考騰訊開放平臺的文檔。xml