友盟第三方登入及分享
阿新 • • 發佈:2019-01-24
一、分享
1.下載友盟的開源庫 social_sdk_library_project
http://download.csdn.net/detail/and_hl/9558667
2. 進行專案的依賴選中自己的專案---->右擊選擇properties---->選擇左側的Android---->在Library節點下Add下載好的social_sdk_library_project的開源庫
3. 在AndroidManifest.xml清單檔案配置許可權
4.在AndroidManifest.xml清單檔案application節點中配置<span style="font-size:18px;"><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.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_LOGS" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.SET_DEBUG_APP" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.USE_CREDENTIALS" /> <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" /></span>
5.建立自己的MyApplication類繼承Application,進行配置<meta-data android:name="UMENG_APPKEY" android:value="4eaee02c527015373b000003" > </meta-data> <!-- 微信 --> <activity android:name="com.umeng.soexample.wxapi.WXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> <!-- 新浪微博 --> <activity android:name=".WBShareActivity" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait" > <intent-filter> <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.sina.weibo.sdk.component.WeiboSdkBrowser" android:configChanges="keyboardHidden|orientation" android:exported="false" android:windowSoftInputMode="adjustResize" > </activity> <service android:name="com.sina.weibo.sdk.net.DownloadService" android:exported="false" > </service> <!-- QQ Qzone --> <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="tencent100424468" /> </intent-filter> </activity> <activity android:name="com.tencent.connect.common.AssistActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
PlatformConfig.setWeixin("wx967daebe835fbeac", "5bb696d9ccd75a38c8a0bfe0675559b3"); // 微信 appid appsecret PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad"); // 新浪微博 appkey appsecret PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba"); // QQ和Qzone appid appkey PlatformConfig.setAlipay("2015111700822536"); // 支付寶 appid PlatformConfig.setYixin("yxc0614e80c9304c11b0391514d09f13bf"); // 易信 appkey PlatformConfig.setTwitter("3aIN7fuF685MuZ7jtXkQxalyi", "MK6FEYG63eWcpDFgRYw4w9puJhzDl0tyuqWjZ3M7XJuuG7mMbO"); // Twitter appid appkey PlatformConfig.setPinterest("1439206"); // Pinterest appid PlatformConfig.setLaiwang("laiwangd497e70d4", "d497e70d4c3e4efeab1381476bac4c5e"); // 來往 appid appkey
6. 在AndroidManifest.xml清單檔案中Application節點下進行引用
android:name="全類名.MyApplication"
7.在Activity中實現分享操作 按鈕的點選事件中
UMImage image = new UMImage(MainActivity.this,
BitmapFactory.decodeResource(getResources(),
android.R.drawable.editbox_dropdown_light_frame));
new ShareAction(this).setDisplayList(displaylist).withText("呵呵")
.withTitle("title").withTargetUrl("http://www.baidu.com")
.withMedia(image).setListenerList(new UMShareListener() {
@Override
public void onResult(SHARE_MEDIA platform) {
Toast.makeText(MainActivity.this, platform + " 分享成功啦",
Toast.LENGTH_SHORT).show();
}
@Override
public void onError(SHARE_MEDIA platform, Throwable t) {
Toast.makeText(MainActivity.this, platform + " 分享失敗啦",
Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel(SHARE_MEDIA platform) {
Toast.makeText(MainActivity.this, platform + " 分享取消了",
Toast.LENGTH_SHORT).show();
}
}).open();
8. 重寫Activity的回撥方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}
9. 成員變數
final SHARE_MEDIA[] displaylist = new SHARE_MEDIA[] { SHARE_MEDIA.WEIXIN,
SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ,
SHARE_MEDIA.QZONE, SHARE_MEDIA.DOUBAN };
二、登入
1.登入按鈕的點選事件
// 獲取UMShareAPI
mShareAPI = UMShareAPI.get(mainActivity);
// 使用哪個平臺賬號登入
final SHARE_MEDIA platform = SHARE_MEDIA.QQ;
mShareAPI.doOauthVerify(mainActivity, platform, new UMAuthListener() {
@Override
public void onError(SHARE_MEDIA arg0, int arg1, Throwable arg2) {
}
@Override
public void onComplete(SHARE_MEDIA arg0, int arg1, Map<String, String> arg2) {
mShareAPI.getPlatformInfo(mainActivity, platform, new UMAuthListener() {
@Override
public void onError(SHARE_MEDIA arg0,
int arg1, Throwable arg2) {
}
@Override
public void onComplete(SHARE_MEDIA arg0,
int arg1, Map<String, String> map) {
// screen_name 網名
// profile_image_url 頭像地址
String url = map.get("profile_image_url");
String name = map.get("screen_name");
System.out.println(url + "-----" + name);
iv_icon.setImageUrl(url);
tv_name.setText(name);
}
@Override
public void onCancel(SHARE_MEDIA arg0, int arg1) {
}
});
}
@Override
public void onCancel(SHARE_MEDIA arg0, int arg1) {
}
});
2. 在回撥方法中新增
mShareAPI.onActivityResult(requestCode, resultCode, data);