Android使用ShareSDK集成QQ、微信、微博等第三方登录

转载请注明出处:http://blog.csdn.net/alpha58/article/details/62424058

前言

第三方登录几乎是每个APP的必须功能。有些人说看官方的文档实在让人眼花缭乱,以前自己第一次做这些功能的时候也会遇到很多坑。这里整理了一份比较详细的,主要给新手学习。有任何问题欢迎提出来!

1、获取ShareSDK的AppKey

(照着流程来,很简单!)
http://bbs.mob.com/forum.php?mod=viewthread&tid=8212&extra=page%3D1

2、下载SDK

下载地址:http://www.mob.com/downloadDetail/ShareSDK/android
下载的时候一般用默认的就足够了
这里写图片描述

下载后解压出来是这样的
这里写图片描述

3、准备资源

点击第一个文件夹,进去如下,双击QuickIntegrater.jar
这里写图片描述

只需要填写项目名称和项目包名即可,其他默认。如图:
这里写图片描述

点击确认后会生成如下文件夹,如图:
这里写图片描述

这里写图片描述

4、开始集成

1,将上面的文件夹依次粘贴到工程相应的文件夹即可,如图:

这里写图片描述

2,在清单文件AndroidManifest.xml添加权限

<uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <!-- 蓝牙分享所需的权限 -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

3,在清单文件AndroidManifest.xml添加activity信息

(注意:tencent后面的appid要保持和您配置的QQ的appid一致)

<activity
     android:name="com.mob.tools.MobUIShell"
     android:theme="@android:style/Theme.Translucent.NoTitleBar"
     android:configChanges="keyboardHidden|orientation|screenSize"
     android:screenOrientation="portrait"
     android:windowSoftInputMode="stateHidden|adjustResize" >

     <intent-filter>
         <data android:scheme="tencent100371282" />
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.BROWSABLE" />
         <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>

    <!-- 调用新浪原生SDK,需要注册的回调activity -->
    <intent-filter>
        <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

    <!--集成line客户端登录授权,需要添如下格式的过滤器-->
    <intent-filter> 
    <data android:scheme="line.1477692153" />
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

 </activity>

如果您集成了微信,还需要添加下面回调的activity处理;

<activity
     android:name=".wxapi.WXEntryActivity"
     android:theme="@android:style/Theme.Translucent.NoTitleBar"
     android:configChanges="keyboardHidden|orientation|screenSize"
     android:exported="true"
     android:screenOrientation="portrait" /> 

4,在刚刚拷贝进去的ShareSDK中替换mob后台申请的Appkey与各个平台申请的key

注意:
mob后台申请的Appkey:就是第一部中获取ShareSDK的AppKey
各个平台申请的key:需要到各个平台申请,例如需要微信分享,则需要到微信开放平台进行申请

这里写图片描述

5,添加登录代码

1,在您程序启动的时候添加初始化代码(注:不要等调用ShareSDK功能之前才初始化)
即自己添加一个类继承Application,在onCreate方法添加下面的代码,如下:

package com.xinhao.thirdpartysharelogin;

import android.app.Application;

import cn.sharesdk.framework.ShareSDK;

/**
 * author           Alpha58
 * date             2017/03/15
 * desc             ${Application}
 * <p>
 * upDateAuthor     $Author$
 * upDate           $Date$
 * upDateDesc       ${TODO}
 */
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        //初始化ShareSDK
        ShareSDK.initSDK(this);
    }
}

注意: 该类需要在清单文件中注册,如图:
这里写图片描述

2,登录activity代码

package com.xinhao.thirdpartysharelogin;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.mob.tools.utils.UIHandler;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

import static android.R.attr.action;


/**
 * author           Alpha58
 * date             2017/03/16
 * desc             ${第三方登录}
 * <p>
 * upDateAuthor     $Author$
 * upDate           $Date$
 * upDateDesc       ${TODO}
 */
public class LoginActivity extends Activity implements PlatformActionListener, Handler.Callback, View.OnClickListener {

    private static final int MSG_ACTION_CCALLBACK = 0;
    private ImageView ivWxLogin;
    private ImageView ivQqLogin;
    private ImageView ivBlog;
    private ProgressDialog progressDialog;

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


    public void initView() {
        ivWxLogin = (ImageView) findViewById(R.id.iv_wx_login);
        ivQqLogin = (ImageView) findViewById(R.id.iv_qq_login);
        ivBlog = (ImageView) findViewById(R.id.iv_blog);
    }


    public void initListener() {
        ivWxLogin.setOnClickListener(this);
        ivQqLogin.setOnClickListener(this);
        ivBlog.setOnClickListener(this);
    }


    public void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_wx_login:
                Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
                wechat.setPlatformActionListener(this);
                wechat.SSOSetting(false);
                authorize(wechat, 1);
                break;
            case R.id.iv_qq_login:
                Platform qq = ShareSDK.getPlatform(QQ.NAME);
                qq.setPlatformActionListener(this);
                qq.SSOSetting(false);
                authorize(qq, 2);
                break;
            case R.id.iv_blog:
                Platform sina = ShareSDK.getPlatform(SinaWeibo.NAME);
                sina.setPlatformActionListener(this);
                sina.SSOSetting(false);
                authorize(sina, 3);
                break;
            default:
                break;
        }

    }

    //授权
    private void authorize(Platform plat, int type) {
        switch (type) {
            case 1:
                showProgressDialog(getString(R.string.opening_wechat));
                break;
            case 2:
                showProgressDialog(getString(R.string.opening_qq));
                break;
            case 3:
                showProgressDialog(getString(R.string.opening_blog));
                break;
        }
        if (plat.isValid()) { //如果授权就删除授权资料
            plat.removeAccount();
        }
        plat.showUser(null);//授权并获取用户信息
    }

    //登陆授权成功的回调
    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> res) {
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 1;
        msg.arg2 = action;
        msg.obj = platform;
        UIHandler.sendMessage(msg, this);   //发送消息

    }

    //登陆授权错误的回调
    @Override
    public void onError(Platform platform, int i, Throwable t) {
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 2;
        msg.arg2 = action;
        msg.obj = t;
        UIHandler.sendMessage(msg, this);
    }

    //登陆授权取消的回调
    @Override
    public void onCancel(Platform platform, int i) {
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 3;
        msg.arg2 = action;
        msg.obj = platform;
        UIHandler.sendMessage(msg, this);
    }

    //登陆发送的handle消息在这里处理
    @Override
    public boolean handleMessage(Message message) {
        hideProgressDialog();
        switch (message.arg1) {
            case 1: { // 成功
                Toast.makeText(LoginActivity.this, "授权登陆成功", Toast.LENGTH_SHORT).show();

                //获取用户资料
                Platform platform = (Platform) message.obj;
                String userId = platform.getDb().getUserId();//获取用户账号
                String userName = platform.getDb().getUserName();//获取用户名字
                String userIcon = platform.getDb().getUserIcon();//获取用户头像
                String userGender = platform.getDb().getUserGender(); //获取用户性别,m = 男, f = 女,如果微信没有设置性别,默认返回null
                Toast.makeText(LoginActivity.this, "用户信息为--用户名:" + userName + "  性别:" + userGender, Toast.LENGTH_SHORT).show();

                //下面就可以利用获取的用户信息登录自己的服务器或者做自己想做的事啦!
                //。。。

            }
            break;
            case 2: { // 失败
                Toast.makeText(LoginActivity.this, "授权登陆失败", Toast.LENGTH_SHORT).show();
            }
            break;
            case 3: { // 取消
                Toast.makeText(LoginActivity.this, "授权登陆取消", Toast.LENGTH_SHORT).show();
            }
            break;
        }
        return false;
    }

    //显示dialog
    public void showProgressDialog(String message) {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(message);
        progressDialog.setCancelable(true);
        progressDialog.show();
    }

    //隐藏dialog
    public void hideProgressDialog() {
        if (progressDialog != null)
            progressDialog.dismiss();
    }

}

3,布局activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:orientation="horizontal"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <ImageView
            android:id="@+id/iv_wx_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:clickable="true"
            android:src="@mipmap/wechat_icon"
            />

        <ImageView
            android:id="@+id/iv_qq_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:clickable="true"
            android:src="@mipmap/qq_icon"
            />

        <ImageView
            android:id="@+id/iv_blog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:clickable="true"
            android:src="@mipmap/blog_icon"
            />


    </LinearLayout>


</LinearLayout>

效果如下:

这里写图片描述

Demo下载:https://github.com/Alpha58/ThirdPartyShareLogin

如果对你有帮助记得点赞,star支持下哈~

wildma_ CSDN认证博客专家 屏幕适配 主流框架源码分析 Android
我是 wildma,CSDN 认证博客专家,简书程序员优秀作者,擅长屏幕适配。Github:https://github.com/wildma
已标记关键词 清除标记
这是我的代码,先点击一个图片按钮,然后跳转到授权界面,当授权完成的时候,再把值返回在这个类里面,但是运行的时候一直报这句left_menu_fragment_textview.setText(userInfo.getUserName());是空指针异常,这是怎么回事 public class LeftMenuFragment extends BaseFragment { private ImageButton left_menu_fragment_imagebutton; private ListView left_menu_fragment_listview; private String[] itemname = new String[] { "设置文字大小", "收藏", "检查更新" }; /************************************ 后面增加的方法 **********************************************/ private OnLoginListener signupListener; private Platform platform; private String picturePath; private UserInfo userInfo = new UserInfo(); /** 加载用户icon */ private static final int LOAD_USER_ICON = 2; /** 图片名字 */ private static final String PICTURE_NAME = "userIcon.jpg"; private TextView left_menu_fragment_textview; /**********************************************************************************/ @Override public View initview() { View view = View.inflate(mActivity, R.layout.left_menu_fragment, null); left_menu_fragment_imagebutton = (ImageButton) view .findViewById(R.id.left_menu_fragment_imagebutton); left_menu_fragment_listview = (ListView) view .findViewById(R.id.left_menu_fragment_listview); left_menu_fragment_textview = (TextView) view .findViewById(R.id.left_menu_fragment_textview); left_menu_fragment_listview.setAdapter(new myListAdapter()); left_menu_fragment_imagebutton .setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.i("pan", "已经点击跳转界面"); Intent intent = new Intent(); intent.setClass(mActivity, AuthorizationLoginActivity.class); mActivity.startActivity(intent); } }); return view; } /** * 初始化数据 */ @Override public void initData() { if (platform != null) { userInfo.setUserIcon(platform.getDb().getUserIcon()); userInfo.setUserName(platform.getDb().getUserName()); Log.i("pan", "leftMenuFragment中的username" + userInfo.getUserName()); Log.i("pan", "leftMenuFragment中的icon" + userInfo.getUserIcon()); if (userInfo.getUserName() != null) { Log.i("pan", "leftMenuFragment中的username222222" + userInfo.getUserName()); left_menu_fragment_textview.setText(userInfo.getUserName()); } } if (!TextUtils.isEmpty(userInfo.getUserIcon())) { Log.i("pan", "执行进入了获取icon方法吗"); loadIcon(); } // 初始化照片保存地址 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { String thumPicture = Environment.getExternalStorageDirectory() .getAbsolutePath() + "/" + mActivity.getPackageName() + "/download"; File pictureParent = new File(thumPicture); File pictureFile = new File(pictureParent, PICTURE_NAME); if (!pictureParent.exists()) { pictureParent.mkdirs(); } try { if (!pictureFile.exists()) { pictureFile.createNewFile(); } } catch (Exception e) { e.printStackTrace(); } picturePath = pictureFile.getAbsolutePath(); Log.e("picturePath ==>>", picturePath); } else { Log.e("change user icon ==>>", "there is not sdcard!"); } // Bundle bundle = mActivity.getIntent().getExtras(); // if (bundle != null) { // // Log.i("pan", "LeftMenuFragment中的res值:" + bundle.get("myMap")); // Map<String, Object> hashmap = (Map<String, Object>) // bundle.getSerializable("myMap"); // String name = (String) hashmap.get("nickname"); // Log.i("pan", "LeftMenuFragment中的res值:" + hashmap); // Log.i("pan", "LeftMenuFragment中的name值:" + name); // } } private Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case LOAD_USER_ICON: left_menu_fragment_imagebutton.setImageURI(Uri .parse(picturePath)); break; default: break; } }; }; /** * 加载头像 */ public void loadIcon() { final String imageUrl = platform.getDb().getUserIcon(); new Thread(new Runnable() { @Override public void run() { try { URL picUrl = new URL(imageUrl); Bitmap userIcon = BitmapFactory.decodeStream(picUrl .openStream()); FileOutputStream b = null; try { b = new FileOutputStream(picturePath); userIcon.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件 } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { b.flush(); b.close(); } catch (IOException e) { e.printStackTrace(); } } userInfo.setUserIcon(picturePath); Message msg = new Message(); msg.what = LOAD_USER_ICON; // UIHandler.sendMessage(msg); handler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } } }).start(); } public boolean handleMessage(Message msg) { switch (msg.what) { case LOAD_USER_ICON: left_menu_fragment_imagebutton.setImageURI(Uri.parse(picturePath)); break; default: break; } return false; } /** * listview的适配器 * * @author Administrator * */ class myListAdapter extends BaseAdapter { private TextView text; @Override public int getCount() { // TODO Auto-generated method stub return itemname.length; } @Override public Object getItem(int position) { return itemname[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = View.inflate(mActivity, R.layout.left_menu_fragment_listview, null); } text = (TextView) convertView .findViewById(R.id.left_menu_fragment_listview_textview); text.setText(itemname[position]); return convertView; } } /****************************************************************************************/ /** 设置授权回调,用于判断是否进入注册 */ public void setOnLoginListener(OnLoginListener l) { this.signupListener = l; } public void setPlatform(String platName) { Log.i("pan", "platName的值为:" + platName); platform = ShareSDK.getPlatform(platName); initData(); } 这下面是打印的值: 10-30 09:11:52.480: I/pan(8062): leftMenuFragment中的username额路的快乐 10-30 09:11:52.480: I/pan(8062): leftMenuFragment中的iconhttp://q.qlogo.cn/qqapp/100371282/C9F7933B7F1457206982CCF2EC3548F9/40 10-30 09:11:52.480: I/pan(8062): leftMenuFragment中的username222222额路的快乐 10-30 09:11:52.480: I/pan(8062): 执行进入了获取icon方法吗 这下面是错误的提示: 10-30 09:11:52.490: E/AndroidRuntime(8062): java.lang.NullPointerException 10-30 09:11:52.490: E/AndroidRuntime(8062): at com.pan.foucstoday.fragment.LeftMenuFragment.initData(LeftMenuFragment.java:121) 10-30 09:11:52.490: E/AndroidRuntime(8062): at com.pan.foucstoday.fragment.LeftMenuFragment.setPlatform(LeftMenuFragment.java:270) 10-30 09:11:52.490: E/AndroidRuntime(8062): at com.pan.foucstoday.AuthorizationLoginActivity.handleMessage(AuthorizationLoginActivity.java:201) 10-30 09:11:52.490: E/AndroidRuntime(8062): at com.mob.tools.utils.UIHandler.handleMessage(Unknown Source) 10-30 09:11:52.490: E/AndroidRuntime(8062): at com.mob.tools.utils.UIHandler.access$000(Unknown Source)
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页