三方分享,登录

1.添加sdk和res

将sdk里面的jar和资源拷贝到项目中:

    1.G:\三方\友盟\sdk\Android\analytics\analytics_android_8.0.2:统计的sdk


    2.G:\三方\友盟\sdk\Android\common\common_android_2.0.2\normal,基础的sdk(必须)


    3.G:\三方\友盟\sdk\Android\share\share_android_6.9.4:分享的sdk


    4.配置完成后:libs+图片+布局+values对应都有相应的资源

2.清单文件

      
    
    
    
    





        
            
                

                
                

                
            
        
        

3.Android6.0权限适配

        if (Build.VERSION.SDK_INT>Build.VERSION_CODES.M){

            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,

                    Manifest.permission.READ_EXTERNAL_STORAGE,};

            ActivityCompat.requestPermissions(this,mPermissionList,123);

        }

4.初始化设置

@Override
    public void onCreate() {
        super.onCreate();
        UMConfigure.init(this,"5a12384aa40fa3551f0001d1","umeng",UMConfigure.DEVICE_TYPE_PHONE,"");
//58edcfeb310c93091c000be2 5965ee00734be40b580001a0
                /**
         * 设置组件化的Log开关
         * 参数: boolean 默认为false,如需查看LOG设置为true
         */
        UMConfigure.setLogEnabled(true);
    }

5.创建BaseApp

public class BaseApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
1       PlatformConfig.setWeixin("wxdc1e388c3822c80b", "3baf1193c85774b3fd9d18447d76cab0");

2      //豆瓣RENREN平台目前只能在服务器端配置

3
        PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad","http://sns.whalecloud.com");
4
        PlatformConfig.setYixin("yxc0614e80c9304c11b0391514d09f13bf");
5
        PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
6
        PlatformConfig.setTwitter("3aIN7fuF685MuZ7jtXkQxalyi", "MK6FEYG63eWcpDFgRYw4w9puJhzDl0tyuqWjZ3M7XJuuG7mMbO");
7
        PlatformConfig.setAlipay("2015111700822536");
8
        PlatformConfig.setLaiwang("laiwangd497e70d4", "d497e70d4c3e4efeab1381476bac4c5e");
9
        PlatformConfig.setPinterest("1439206");
10
        PlatformConfig.setKakao("e4f60e065048eb031e235c806b31c70f");
11
        PlatformConfig.setDing("dingoalmlnohc0wggfedpk");
12
        PlatformConfig.setVKontakte("5764965","5My6SNliAaLxEm3Lyd9J");
13
        PlatformConfig.setDropbox("oz8v5apet3arcdy","h7p2pjbzkkxt02a");
14
        PlatformConfig.setYnote("9c82bf470cba7bd2f1819b0ee26f86c6ce670e9b");
    }
}

6.签名在依赖中添加

android {
   ...
    signingConfigs {
        debug {
        storeFile file('debug.keystore')
        storePassword "android"
        keyAlias "androiddebugkey"
        keyPassword "android"
        }
    }
}

7.分享

分享(带面板,带图片)
    public void shareBorad(){
        UMImage image = new UMImage(MainActivity.this, "http://ww2.sinaimg.cn/large/7a8aed7bgw1essxtqxs6jj20mj0tzdhl.jpg");
        image.compressStyle = UMImage.CompressStyle.QUALITY;
        new ShareAction(MainActivity.this)
                .withText("hello")
                .withMedia(image)
                .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
                .setCallback(shareListener).open();
    }
分享(不带面板,带图片)
   private void share(SHARE_MEDIA media) {
        UMImage image = new UMImage(MainActivity.this, "http://ww2.sinaimg.cn/large/7a8aed7bgw1essxtqxs6jj20mj0tzdhl.jpg");
        image.compressStyle = UMImage.CompressStyle.QUALITY;
        new ShareAction(MainActivity.this)
                .setPlatform(media)//传入平台
                .withText("hello")//分享内容
                .withMedia(image)
                .setCallback(shareListener)//回调监听器
                .share();
    }


//分享
    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(MainActivity.this, "成功了", Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(MainActivity.this, "失败" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_LONG).show();

        }
    };

8.登录

    private void login(SHARE_MEDIA mode) {
        UMShareAPI umShareAPI = UMShareAPI.get(this);
        umShareAPI.getPlatformInfo(MainActivity.this, mode, authListener);
    }


//登录
    UMAuthListener authListener = new UMAuthListener() {
        /**
         * @desc 授权开始的回调
         * @param platform 平台名称
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @desc 授权成功的回调
         * @param platform 平台名称
         * @param action 行为序号,开发者用不上
         * @param data 用户资料返回
         */
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map data) {

            Toast.makeText(MainActivity.this, "成功了", Toast.LENGTH_LONG).show();

        }

        /**
         * @desc 授权失败的回调
         * @param platform 平台名称
         * @param action 行为序号,开发者用不上
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {

            Toast.makeText(MainActivity.this, "失败:" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @desc 授权取消的回调
         * @param platform 平台名称
         * @param action 行为序号,开发者用不上
         */
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_LONG).show();
        }
    };

9.内存泄漏解决方案

@Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }

你可能感兴趣的:(三方分享,登录)