第三方登录

AndroidManifest.xml:



    
    
    
    
    
    
    

    
    
        
            
                

                
            
        

        
        

            
                
                
                
                
            

        

        
        
        
        
    

activity_main.xml:




    

MyApplication.java:

package com.umeng.soexample;

import android.app.Application;

import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;

/**
 * 第一步:导入jar包和res文件,将jar包add as lib..
 * 第二步:自定义Application 在onCreate中完成以下两行代码(所有参数没有自己注册过不要改)
 * 第三步:在Manifests中添加配置,详见Manifests注释(不要随意修改)
 * 第四步:在需要调用的地方写调用,详见MainActivity中
 */
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        UMConfigure.init(this,"5a12384aa40fa3551f0001d1","umeng",UMConfigure.DEVICE_TYPE_PHONE,"");
        PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
    }
}

MainActivity.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button QQ_Share;
    private Button QQ_login;


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

    /**
     * 添加回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    private void initView() {
        QQ_Share = findViewById(R.id.QQ_Share);
        QQ_login = findViewById(R.id.QQ_login);

        QQ_Share.setOnClickListener(this);
        QQ_login.setOnClickListener(this);

        checkPermission();
    }

    /**
     * 动态添加权限,模拟器Android版本小于6.0的不用写,再有问的打死
     */
    private void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.CALL_PHONE,
                    Manifest.permission.READ_LOGS,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.SET_DEBUG_APP,
                    Manifest.permission.SYSTEM_ALERT_WINDOW,
                    Manifest.permission.GET_ACCOUNTS,
                    Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this, mPermissionList, 123);
        }
    }

    /**
     * 动态添加权限回调,模拟器Android版本小于6.0的不用写,再有问的打死
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.QQ_Share:
                //分享用的图片
                UMImage image = new UMImage(MainActivity.this, R.drawable.umeng_socialize_qq);
                new ShareAction(MainActivity.this)
                        //分享的标题
                        .withText("hello")
                        //分享的图片
                        .withMedia(image)
                        //分享到哪,这里面只写了QQ,如有需要在后面添加
                        .setDisplayList(SHARE_MEDIA.QQ)
                        //设置回调
                        .setCallback(shareListener)
                        //打开分享面板
                        .open();
                break;
            case R.id.QQ_login:
                //获得UMShareAPI实例
                UMShareAPI umShareAPI =  UMShareAPI.get(MainActivity.this);

                //开始登录
                //第一个参数:上下文
                //第二个参数,登录哪种平台
                //第三个参数,添加回调
                umShareAPI.getPlatformInfo(MainActivity.this, SHARE_MEDIA.QQ, new UMAuthListener() {
                    /**
                     * 开始登录回调
                     * @param share_media
                     */
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        Log.i("dj", "UMAuthListener onStart");
                    }

                    /**
                     * 登录完成
                     * @param share_media
                     * @param i
                     * @param map
                     */
                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map map) {
                        //头像,昵称,如果拿不到,自己debug看Key是啥,再问打死
                        Log.i("dj", "UMAuthListener onComplete");

                        //获取名字
                        String name  = map.get("screen_name");
                        //获取头像
                        String img  = map.get("profile_image_url");

                        Log.i("dj", "name is "+ name);
                        Log.i("dj", "img is "+ img);
                    }

                    /**
                     * 登录失败
                     * @param share_media
                     * @param i
                     * @param throwable
                     */
                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                        Log.i("dj", "UMAuthListener onError" + throwable.getLocalizedMessage());
                    }

                    /**
                     * 登录取消
                     * @param share_media
                     * @param i
                     */
                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {
                        Log.i("dj", "UMAuthListener onCancel");
                    }
                });
                break;
            default:
                break;
        }
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
            Log.i("dj", "UMShareListener onStart");
        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.i("dj", "UMShareListener onResult");
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Log.i("dj", "UMShareListener onError");
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Log.i("dj", "UMShareListener onCancel");
        }
    };
}

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