1. 程式人生 > >umeng第三方QQ登入實現

umeng第三方QQ登入實現

MainActivity

package com.umeng.soexample;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
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 java.util.Map;

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<String, String> 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");
        }
    };
}

MainActivity2登入

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

    //QQ登入
    private void initView() {
        QQ_login = findViewById(R.id.qq_login);
        QQ_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UMShareAPI umShareAPI =  UMShareAPI.get(MainActivity.this);
                umShareAPI.getPlatformInfo(MainActivity.this, SHARE_MEDIA.QQ, new UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {

                    }

                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                        //獲取名字
                        String name  = map.get("screen_name");
                        //獲取頭像
                        String img  = map.get("profile_image_url");

                        Log.i("zjh", "暱稱 是 "+ name);
                        Log.i("zjh", "頭像 是 "+ img);
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {

                    }
                });
            }
        });
    }

Application

package com.zjh.administrat.myweekdemo2;

import android.app.Application;

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

public class MyApplition extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        UMConfigure.init(this,"5a12384aa40fa3551f0001d1","umeng",UMConfigure.DEVICE_TYPE_PHONE,"");
        PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
    }
}

許可權如下

<!-- 新增以下許可權 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.INTERNET" />

Manifest中</ >

<!-- 3.寫以下兩個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="tencent100424468" />
            </intent-filter>

        </activity>

        <activity
            android:name="com.tencent.connect.common.AssistActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="orientation|keyboardHidden|screenSize"/>
        <!-- 3.寫以下配置 -->
        <meta-data android:name="UMENG_APPKEY"
            android:value="5c07d385f1f556e5f90003c4">
        </meta-data>
             <activity android:name=".ZhuceActivity" />
             <activity android:name=".EmailActivity">
        </activity>

佈局.xml

umeng佈局檔案

lib中匯入jar檔案

jar包

drawable各種圖片

按需要自行使用