友盟Android 社會化組件SDK v5.0.4中qq,微信,新浪微博的集成

友盟Android 社會化組件SDK v5.0.4版本新增QQ、微信、新浪分享迴流率統計功能,根據需求將最新版的友盟社會化組件SDK從新封裝了一下。html

根據友盟的開發者文檔中的步驟一步一步來。需求是在原來老版本的基礎上進行的改版,這裏只介紹與原來4.0版本的區別。java

第一步:react

在Application的Java文件裏設置各個平臺的配置。android

第二步:
json


這不一樣顏色是新版本更新的sdk的jar包,須要加入到項目的libs文件裏。api

第三步:微信

將libweibosdkcore.so文件分別添加到以上7個文件夾裏,注意在android studio裏so文件不能和libs中的jar文件放在一塊,不然會出現沒有安裝新浪微博客戶端時,分享崩潰的問題。app

第四步:ide

<!-- *************************    分享相關的註冊 START **************************** -->
<!-- Share edit page-->
<activity
   android:name=".weibo.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>
<!-- 分享內容編輯頁 -->
<activity
   android:name="com.umeng.socialize.editorpage.ShareActivity"
   android:excludeFromRecents="true"
   android:theme="@style/Theme.UMDefault"/>
<!-- 微信回調activity -->
<activity
   android:name=".wxapi.WXEntryActivity"
   android:configChanges="keyboardHidden|orientation|screenSize"
   android:exported="true"
   android:screenOrientation="portrait"
   android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
<!-- ############ QQ空間和QQ SSO受權的Activity註冊 ############ -->
<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="tencent1104233438"/>
   </intent-filter>
</activity>
<activity
   android:name="com.tencent.connect.common.AssistActivity"
   android:screenOrientation="portrait"
   android:theme="@android:style/Theme.Translucent.NoTitleBar"/>

<uses-library
   android:name="com.google.android.maps"
   android:required="false"/>
<uses-library android:name="android.test.runner"/>

須要在配置清單文件裏添加以上配置,方法在友盟開發文檔裏有詳細介紹。ui

第五步:

開發文檔中各類平臺的受權示例代碼以下:

package com.umeng.soexample.share_auth;
/**
 * Created by wangfei on 15/9/14.
 */
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.utils.Log;
import com.umeng.soexample.R;

import java.util.Map;

public class AuthActivity extends Activity{

        private UMShareAPI mShareAPI = null;

        public void onClickAuth(View view) {
            SHARE_MEDIA platform = null;
            if (view.getId() == R.id.app_auth_sina){
                platform = SHARE_MEDIA.SINA;
            }else if (view.getId() == R.id.app_auth_renren){
                platform = SHARE_MEDIA.RENREN;
            }else if (view.getId() == R.id.app_auth_douban){
                platform = SHARE_MEDIA.DOUBAN;
            }else if (view.getId() == R.id.app_auth_qq){
                platform = SHARE_MEDIA.QQ;
            }else if (view.getId() == R.id.app_auth_weixin){
                platform = SHARE_MEDIA.WEIXIN;
            }else if (view.getId() == R.id.app_auth_facebook){
                platform = SHARE_MEDIA.FACEBOOK;
            }else if (view.getId() == R.id.app_auth_twitter){
                platform = SHARE_MEDIA.TWITTER;
            }else if (view.getId() == R.id.app_auth_tencent){
                platform = SHARE_MEDIA.TENCENT;
            }else if(view.getId() == R.id.app_auth_linkedin){
                platform = SHARE_MEDIA.LINKEDIN;
            }
            /**begin invoke umeng api**/

            mShareAPI.doOauthVerify(AuthActivity.this, platform, umAuthListener);
        }

    public void onClickDeletAuth(View view) {
        SHARE_MEDIA platform = null;
        if (view.getId() == R.id.app_del_auth_sina){
            platform = SHARE_MEDIA.SINA;
        }else if (view.getId() == R.id.app_del_auth_renren){
            platform = SHARE_MEDIA.RENREN;
        }else if (view.getId() == R.id.app_del_auth_douban){
            platform = SHARE_MEDIA.DOUBAN;
        }else if (view.getId() == R.id.app_del_auth_qq){
            platform = SHARE_MEDIA.QQ;
        }else if (view.getId() == R.id.app_del_auth_weixin){
            platform = SHARE_MEDIA.WEIXIN;
        }else if (view.getId() == R.id.app_del_auth_facebook){
            platform = SHARE_MEDIA.FACEBOOK;
        }else if (view.getId() == R.id.app_del_auth_twitter){
            platform = SHARE_MEDIA.TWITTER;
        }else if(view.getId() == R.id.app_auth_linkedin_del){
            platform = SHARE_MEDIA.LINKEDIN;
        }
        /**begin invoke umeng api**/
        mShareAPI.deleteOauth(AuthActivity.this, platform, umdelAuthListener);
    }
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_auth);
        /** init auth api**/
        mShareAPI = UMShareAPI.get( this );
    }
    /** auth callback interface**/
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };
    /** delauth callback interface**/
    private UMAuthListener umdelAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            Toast.makeText(getApplicationContext(), "delete Authorize succeed", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "delete Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "delete Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("auth", "on activity re 2");
        mShareAPI.onActivityResult(requestCode, resultCode, data);
        Log.d("auth", "on activity re 3");
    }
}

開發文檔中各類平臺的分享示例代碼以下:

package com.umeng.soexample.share_auth;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.umeng.socialize.Config;
import com.umeng.socialize.ShareAction;
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.UMVideo;
import com.umeng.socialize.media.UMusic;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;
import com.umeng.soexample.R;

/**
 * Created by umeng on 15/9/14.
 */
public class ShareActivity extends Activity{
    private CheckBox cb;

    public void onClick(View view) {

        UMImage image = new UMImage(ShareActivity.this, "http://d.hiphotos.baidu.com/zhidao/pic/item/9358d109b3de9c8252c0eeac6e81800a19d8436f.jpg");

//        UMImage image = new UMImage(ShareActivity.this, "http://blog.thegmic.com/wp-content/uploads/2012/05/umeng.jpg");
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.info_icon_1);
//
//        UMImage image = new UMImage(ShareActivity.this, bitmap);
        //UMImage image1 = new UMImage(ShareActivity.this,new File("/storage/emulated/0/umeng_cache/0EADEEDFC79B32EB58E871D44E6DAA91"));

        // two line below are for testing on nexus 5 only, don't use it on other phones
        //UMImage image = new UMImage(ShareActivity.this,new File("/storage/emulated/0/DCIM/1453176647573.jpg"));
        //UMVideo video2 = new UMVideo("/storage/emulated/0/DCIM/test.mp4");

        UMusic music = new UMusic("http://music.huoxing.com/upload/20130330/1364651263157_1085.mp3");
        music.setTitle("This is music title");
        music.setThumb(new UMImage(ShareActivity.this, "http://www.umeng.com/images/pic/social/chart_1.png"));
        UMVideo video = new UMVideo("http://video.sina.com.cn/p/sports/cba/v/2013-10-22/144463050817.html");
        String url = "http://www.umeng.com";
        switch (view.getId()){

            case R.id.app_open_share:
                /**shareboard  need the platform all you want and callbacklistener,then open it**/
                new ShareAction(this).setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE)
                        .withText("來自友盟分享面板")
                        .withMedia(image)
                        .setCallback(umShareListener)
                        .open();
                break;
            case R.id.app_open_share_custom:
                new ShareAction(this).setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE)
                        .addButton("umeng_sharebutton_custom","umeng_sharebutton_custom","info_icon_1","info_icon_1")
                        .setShareboardclickCallback(new ShareBoardlistener() {
                            @Override
                            public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                                if (snsPlatform.mShowWord.equals("umeng_sharebutton_custom")){
                                    Toast.makeText(ShareActivity.this,"自定義按鈕",Toast.LENGTH_LONG).show();
                                }else {
                                    new ShareAction(ShareActivity.this).withText("來自友盟自定義分享面板")
                                            .setPlatform(share_media)
                                            .setCallback(umShareListener)
                                            .share();


                                }
                            }
                        }).open();
                break;
           case R.id.app_share_sina:
                /** shareaction need setplatform , callbacklistener,and content(text,image).then share it **/
               new ShareAction(this).setPlatform(SHARE_MEDIA.SINA).setCallback(umShareListener)
                       .withTitle("title")
                       .withText("umeng")
                       .withMedia(image)
                       .withTargetUrl("http://dev.umeng.com")
                       .share();
               break;
           case R.id.app_share_douban:

               new ShareAction(this).setPlatform(SHARE_MEDIA.DOUBAN).setCallback(umShareListener)
                       .withText("hello umeng")
//                       .withMedia(image)
                       .share();
               break;

           case R.id.app_share_email:
               new ShareAction(this).setPlatform(SHARE_MEDIA.EMAIL).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(music)
                       .withTitle("dddddd")
                       .share();
               break;
           case R.id.app_share_wx:

               new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN).setCallback(umShareListener)
                        .withMedia(image)
                        .withText("hello umeng")
                        .withTargetUrl("http://dev.umeng.com")
                       .share();
               break;
           case R.id.app_share_wx_circle:

               new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(umShareListener)
                       .withMedia(image)
                       .share();
               break;
           case R.id.app_share_sms:
               new ShareAction(this).setPlatform(SHARE_MEDIA.SMS).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(image)
                       .share();
               break;
           case R.id.app_share_qq:
               new ShareAction(this).setPlatform(SHARE_MEDIA.QQ).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(music)
                       .withTitle("qqshare")
                       .withTargetUrl("http://dev.umeng.com")
                       .share();
               break;
           case R.id.app_share_qzone:
               new ShareAction(this).setPlatform(SHARE_MEDIA.QZONE).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(new UMImage(ShareActivity.this, R.drawable.ic_launcher))
                       .share();
               break;
           case R.id.app_share_yixin:
               new ShareAction(this).setPlatform(SHARE_MEDIA.YIXIN).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(image)
                       .share();
               break;
           case R.id.app_share_yixin_circle:
               new ShareAction(this).setPlatform(SHARE_MEDIA.YIXIN_CIRCLE).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(image)
                       .share();
               break;
           case R.id.app_share_ynote:
               new ShareAction(this).setPlatform(SHARE_MEDIA.YNOTE).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(image)
                       .share();
               break;
           case R.id.app_share_evernote:
               new ShareAction(this).setPlatform(SHARE_MEDIA.EVERNOTE).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(image)
                       .share();
               break;
           case R.id.app_share_facebook:
               new ShareAction(this).setPlatform(SHARE_MEDIA.FACEBOOK).setCallback(umShareListener)
                       .withTargetUrl(url)
                       .withMedia(image)
                       .share();
               break;
           case R.id.app_share_laiwang:
               new ShareAction(this).setPlatform(SHARE_MEDIA.LAIWANG).setCallback(umShareListener)
                       .withText("hello umeng")
                      .withMedia(image)
                       .share();
               break;
           case R.id.app_share_line:
               new ShareAction(this).setPlatform(SHARE_MEDIA.LINE).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(image)
                       .share();
               break;
           case R.id.app_share_linkedin:
               new ShareAction(this).setPlatform(SHARE_MEDIA.LINKEDIN).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withTitle("this is cool")
                       .withTargetUrl("https://www.baidu.com/")
                       .share();
               break;
           case R.id.app_share_twitter:
               new ShareAction(this).setPlatform(SHARE_MEDIA.TWITTER).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(image)
                       .share();
               break;
           case R.id.app_share_tencent:
               new ShareAction(this).setPlatform(SHARE_MEDIA.TENCENT).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(image)
                       .share();
               break;
           case R.id.app_share_kakao:
               new ShareAction(this).setPlatform(SHARE_MEDIA.KAKAO).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(image)
                       .share();
               break;
           case R.id.app_share_alipay:
               new ShareAction(this).setPlatform(SHARE_MEDIA.ALIPAY).setCallback(umShareListener)
                       .withText("hello umeng")
                       .withMedia(image)
                       .share();
//               new ShareAction(this).setPlatform(SHARE_MEDIA.RENREN).setCallback(umShareListener)
//                       .withText("hello umeng")
//                       .withMedia(image)
//                       .share();
               break;
       }
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_share);
        /** need not init ,but must config App.java**/

        cb = (CheckBox) findViewById(R.id.checkBox_close_editor);
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    Config.OpenEditor = true;
                } else {
                    Config.OpenEditor = false;//open editpage
                }
            }
        });

    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {

            Toast.makeText(ShareActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(ShareActivity.this,platform + " 分享失敗啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(ShareActivity.this,platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    private ShareBoardlistener shareBoardlistener = new ShareBoardlistener() {

        @Override
        public void onclick(SnsPlatform snsPlatform,SHARE_MEDIA share_media) {
            new ShareAction(ShareActivity.this).setPlatform(share_media).setCallback(umShareListener)
                    .withText("多平臺分享")
                    .share();
        }
    };
    private UMShareListener testmulListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(ShareActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(ShareActivity.this,platform + " 分享失敗啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(ShareActivity.this, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

}

開發文檔中各類平臺的獲取用戶信息示例代碼以下:

package com.umeng.soexample.share_auth;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.utils.Log;
import com.umeng.socialize.view.UMFriendListener;
import com.umeng.soexample.R;

import java.util.Map;

/**
 * Created by wangfei on 15/10/10.
 */
public class UserinfoActivity extends Activity{
    private UMShareAPI mShareAPI = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_user);
        mShareAPI = UMShareAPI.get(this);
    }
    public void onClick(View view) {
        SHARE_MEDIA platform = null;
        switch (view.getId()){
            case R.id.sinainfo:
                platform = SHARE_MEDIA.SINA;
                break;
            case R.id.qqinfo:
                platform = SHARE_MEDIA.QQ;
                break;
            case R.id.facebookinfo:
                platform = SHARE_MEDIA.LINKEDIN;
                break;
            case R.id.wxinfo:
                platform = SHARE_MEDIA.WEIXIN;
                break;
            case R.id.getfriend:
                break;

        }
        mShareAPI.getPlatformInfo(UserinfoActivity.this, platform, umAuthListener);

    }
    public void getFriendbyClick(View view) {

        mShareAPI.getFriend(UserinfoActivity.this, SHARE_MEDIA.SINA, umGetfriendListener);

    }
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
//            Log.d("auth callbacl","getting data"+data.toString());
            if (data!=null){
                Log.d("auth callbacl","getting data");
                Toast.makeText(getApplicationContext(), data.toString(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "get fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "get cancel", Toast.LENGTH_SHORT).show();
        }
    };
    private UMFriendListener umGetfriendListener = new UMFriendListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, Object> data) {
            if (data!=null){
                Toast.makeText(getApplicationContext(), data.get("json").toString(), Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "get fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "get cancel", Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    }
}

以上就是友盟最新sdk社會化組件的集成。

相關文章
相關標籤/搜索