安卓友盟第三方登入與分享
阿新 • • 發佈:2019-01-10
前言:如果已經整合好SDK,可以直接從7開始
2、解壓後開啟share,再雙擊 友盟整合工具.jar 如下圖
3、點選OK後會生成一個資料夾 如下圖
4、雙擊該資料夾 ,點開libs 將裡面jar包全部複製到自己專案中libs下 如下圖
6、以QQ為例:騰訊開放平臺註冊,新增應用得到APP ID APP KEY
7、清單檔案 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.helloworld.com.umengdemo">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 友盟分享 -->
<!-- 微信 -->
<activity
android:name="wxapi.WXEntryActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- 新浪微博 -->
<activity
android:name="wxapi.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>
<!-- qq精簡版 -->
<activity
android:name="com.umeng.qq.tencent.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" />
<!-- 這裡的scheme是qq分享要用的,如:tencent11011011
<data android:scheme="tencentappid" />
</intent-filter>
</activity>
<activity
android:name="com.umeng.qq.tencent.AssistActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- 分享編輯頁 -->
<activity
android:name="com.umeng.socialize.editorpage.ShareActivity"
android:excludeFromRecents="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data
android:name="UMENG_APPKEY"
android:value="步驟5中的友盟AppKey"/>
</application>
</manifest>
8、新建資料夾wxapi 分享回撥類
WBShareActivity WXEntryActivity
package wxapi;
import com.umeng.socialize.media.WBShareCallBackActivity;
/**
* 微博分享回撥
*/
public class WBShareActivity extends WBShareCallBackActivity {
}
package wxapi;
import com.umeng.weixin.callback.WXCallbackActivity;
/**
* 微信分享回撥
*/
public class WXEntryActivity extends WXCallbackActivity {
}
9、MyApplication 用於初始化友盟SDK
package com.helloworld.com.umengdemo;
import android.app.Application;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化友盟SDK
UMShareAPI.get(this);//初始化sd
//開啟debug模式,方便定位錯誤,具體錯誤檢查方式可以檢視
//http://dev.umeng.com/social/android/quick-integration的報錯必看,正式釋出,請關閉該模式
Config.DEBUG = true;
//微信
PlatformConfig.setWeixin("wxdc1e388c1111c80b", "3baf1193c85774b3fd9d11117d76cab0");
//新浪微博(第三個引數為回撥地址)
PlatformConfig.setSinaWeibo("3111100954", "04b48b094faeb16683c32111124ebdad",
"http://sns.whalecloud.com/sina2/callback");
//應用寶開放平臺 APP ID APP KEY
PlatformConfig.setQQZone("1111114798", "aEIMjd6vWSADA5ol");
}
}
10、MainActivity
package com.helloworld.com.umengdemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
//之所以要刪除授權,是因為每次都拉取對應授權頁
//否則是直接授權並不能切換其他賬號登入
umengDeleteOauth(SHARE_MEDIA.QQ);
//開始授權
shareLoginUmeng(this, SHARE_MEDIA.QQ);
//shareLoginUmeng(this, SHARE_MEDIA.WEIXIN);
//shareLoginUmeng(this, SHARE_MEDIA.SINA);
break;
case R.id.button2:
//以QQ為例
shareWeb(this, "http://www.baidu.com", "Hello Word", "Word", SHARE_MEDIA.QQ);
//shareWeb(this, "", "Hello Word", "Word", SHARE_MEDIA.SINA);
//shareWeb(this, "", "Hello Word", "Word", SHARE_MEDIA.WEIXIN);
break;
}
}
/**
* 友盟開始授權(登入)
*/
public static void shareLoginUmeng(final Activity activity, SHARE_MEDIA share_media_type) {
UMShareAPI.get(activity).getPlatformInfo(activity, share_media_type, new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
Log.e(TAG, "onStart授權開始: ");
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
//sdk是6.4.5的,但是獲取值的時候用的是6.2以前的(access_token)才能獲取到值,未知原因
String uid = map.get("uid");
String openid = map.get("openid");//微博沒有
String unionid = map.get("unionid");//微博沒有
String access_token = map.get("access_token");
String refresh_token = map.get("refresh_token");//微信,qq,微博都沒有獲取到
String expires_in = map.get("expires_in");
String name = map.get("name");//名稱
String gender = map.get("gender");//性別
String iconurl = map.get("iconurl");//頭像地址
Log.e(TAG, "onStart授權完成: " + openid);
Log.e(TAG, "onStart授權完成: " + unionid);
Log.e(TAG, "onStart授權完成: " + access_token);
Log.e(TAG, "onStart授權完成: " + refresh_token);
Log.e(TAG, "onStart授權完成: " + expires_in);
Log.e(TAG, "onStart授權完成: " + uid);
Log.e(TAG, "onStart授權完成: " + name);
Log.e(TAG, "onStart授權完成: " + gender);
Log.e(TAG, "onStart授權完成: " + iconurl);
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
Toast.makeText(activity, "授權失敗", Toast.LENGTH_LONG).show();
Log.e(TAG, "onError: " + "授權失敗");
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
Toast.makeText(activity, "授權取消", Toast.LENGTH_LONG).show();
Log.e(TAG, "onError: " + "授權取消");
}
});
}
/**
* 友盟取消授權(登入)
*/
private void umengDeleteOauth(SHARE_MEDIA share_media_type) {
UMShareAPI.get(this).deleteOauth(this, share_media_type, new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
//開始授權
Log.e(TAG, "onStart: ");
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
//取消授權成功 i=1
Log.e(TAG, "onComplete: ");
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
//授權出錯
Log.e(TAG, "onError: ");
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
//取消授權
Log.e(TAG, "onCancel: ");
}
});
}
/**
* 友盟分享
* 上下文activity、分享的連結、標題、內容、型別
* 若是要分享視訊、音樂可看官方文件
*/
public static void shareWeb(final Activity activity, String WebUrl, String title, String description, SHARE_MEDIA
platform) {
UMImage thumb = new UMImage(activity, R.mipmap.ic_launcher);
UMWeb web = new UMWeb(WebUrl);//連線地址(注意連結開頭必須包含http)
web.setTitle(title);//標題
web.setDescription(description);//描述
web.setThumb(thumb);//縮圖
new ShareAction(activity)
//分享的平臺
.setPlatform(platform)
.withMedia(web)
.setCallback(new UMShareListener() {
/**
* @descrption 分享開始的回撥
* @param share_media 平臺型別
*/
@Override
public void onStart(SHARE_MEDIA share_media) {
Log.e(TAG, "onStart開始分享的平臺: " + share_media);
}
/**
* @descrption 分享成功的回撥
* @param share_media 平臺型別
*/
@Override
public void onResult(final SHARE_MEDIA share_media) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, " 分享成功 ", Toast.LENGTH_SHORT).show();
Log.e(TAG, "onStart分享成功的平臺: " + share_media);
}
});
}
/**
* @descrption 分享失敗的回撥
* @param share_media 平臺型別
* @param throwable 錯誤原因
*/
@Override
public void onError(final SHARE_MEDIA share_media, final Throwable throwable) {
if (throwable != null) {
//失敗原因
Log.e(TAG, "throw:" + throwable.getMessage());
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, share_media + " 分享失敗", Toast.LENGTH_SHORT).show();
}
});
}
/**
* @descrption 分享取消的回撥
* @param share_media 平臺型別
*/
@Override
public void onCancel(final SHARE_MEDIA share_media) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, " 分享取消 ", Toast.LENGTH_SHORT).show();
}
});
}
})
.share();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
Log.e(TAG, "onActivityResult: " + requestCode + "\n" + resultCode + "\n" + data);
}
}
對應的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.helloworld.com.umengdemo.MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="QQ、微信、微博登入" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="QQ、微信、微博分享" />
</LinearLayout>
11、回撥後的列印以及遇到的問題
剛開始的時候報錯如下圖
type表明你缺少的檔案型別,可能是string,drawable,layout等資源型別,你只需要在你下載的包中對應的string檔案或佈局檔案的資料夾中去找即可。
這裡是缺少string資源, 在專案values-strings目錄下新增下面2句即可
<string name="umeng_socialize_female">女</string>
<string name="umeng_socialize_male">男</string>