Umeng第三方登录

Umeng第三方登录_第1张图片

首先我们得自己注册一个Umeng :http://www.umeng.com/codecenter.html

分享一下下载SDK网址: http://dev.umeng.com/social/android/sdk-download

Umeng第三方登录_第2张图片

//下载SDK
Umeng第三方登录_第3张图片

//这是下载完SDK需要导入到自己的项目
//把drawable复制到自己demo的drawable
//把layout复制到自己demo的layout
//把libs复制到自己demo的libs
//把values复制到自己demo的values
//把debug.keystore直接复制到自己的demo
Umeng第三方登录_第4张图片

//我们把libs里面的文件导入到咱们的libs里 别忘了添加小奶瓶
Umeng第三方登录_第5张图片

//values文件夹只需要导入 带umeng开头的xml
Umeng第三方登录_第6张图片

//.然后再清单文件进行配置















    
        
            

            
        
    

    
    
    

    
    
    
        
            

            
            

            
        
    
    

```
package com.example.disanfang;

import android.app.Application;

import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
 
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        UMShareAPI.get(this);    
    }

    //里面数据都是固定的不能修改
    {
        PlatformConfig.setQQZone("1106036236","mjFCi0oxXZKZEWJs");
    }
}

///    布局



    

        

        
    

    


package com.example.disanfang;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
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.shareboard.SnsPlatform;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private ImageView iv_login;
    private TextView tv_result;
    //A.定义装平台的容器
    public ArrayList platforms = new ArrayList();
    private SHARE_MEDIA[] list = {SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE};
    private UMShareAPI mUMShareAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_login = (ImageView) findViewById(R.id.iv_login);
        tv_result = (TextView) findViewById(R.id.tv_result);

        initPlatforms();

        //A.获取UM的对象
        mUMShareAPI = UMShareAPI.get(MainActivity.this);

        //A.获取是否授权
        final boolean isauth = UMShareAPI.get(this).isAuthorize(this, platforms.get(0).mPlatform);

        //A.点击QQ的头像,进行授权
        iv_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isauth){
                    Toast.makeText(MainActivity.this, "授权成功", Toast.LENGTH_SHORT).show();
                    mUMShareAPI.deleteOauth(MainActivity.this, platforms.get(0).mPlatform,authListener);
                }else{
                    mUMShareAPI.doOauthVerify(MainActivity.this, platforms.get(0).mPlatform,authListener);
                }
                mUMShareAPI.getPlatformInfo(MainActivity.this, platforms.get(0).mPlatform,authListener);

            }
        });

        //B.分享的逻辑代码
        ImageView iv_share = (ImageView) findViewById(R.id.iv_share);

        final UMImage image = new UMImage(MainActivity.this, "http://b.hiphotos.baidu.com/zhidao/pic/item/63d9f2d3572c11df28e42e30602762d0f703c2e8.jpg");//网络图片
        final UMImage imagelocal = new UMImage(this, R.mipmap.ic_launcher);
        imagelocal.setThumb(new UMImage(this, R.mipmap.ic_launcher));
        imagelocal.setTitle("原来你才是我最疼爱的人");
        iv_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new ShareAction(MainActivity.this).withMedia(image)
                        .setPlatform(platforms.get(0).mPlatform)
                        .setCallback(shareListener).share();

                new ShareAction(MainActivity.this).setPlatform(SHARE_MEDIA.QQ)
                        .withText("hello")
                        .setCallback(shareListener)
                        .share();
            }
        });
    }
    //A.
    private void initPlatforms() {
        //A.集合清空
        platforms.clear();
        //A.通过for循环,把数组数据添加到集合中
        for (SHARE_MEDIA e : list) {
            if (!e.toString().equals(SHARE_MEDIA.GENERIC.toString())) {
                platforms.add(e.toSnsPlatform());
            }
        }
    }

    //A.
    UMAuthListener authListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //授权开始的回调,可以用来处理等待框,或相关的文字提示
        }

        @Override//授权成功时回调
        public void onComplete(SHARE_MEDIA platform, int action, Map data) {
            //获取用户授权后的信息
            Set strings = data.keySet();
            data.get("profile_image_url");
            String temp="";
            for(String key: strings ){
                temp =temp +key +" :" +data.get(key) +"\n";
            }
            tv_result.setText(temp);
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText(MainActivity.this, "失败:" + t.getMessage(), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_LONG).show();
        }
    };

    //A.
    @Override
    protected void onActivityResult ( int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }


    //B.分享的逻辑代码
    private UMShareListener shareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(MainActivity.this, "成功了", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(MainActivity.this, "失败" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_LONG).show();

        }
    };
}

//把第三方登录的图片数据设置为头像

 			String iconurl = data.get( "iconurl" );
            Glide.with(MainActivity.this).load(iconurl).into(image);

你可能感兴趣的:(Umeng第三方登录)