2020超詳細圖解教程android如何使用mob平臺+ShareSDK實現(xiàn)QQ登錄以及分享功能?

如果想了解更多android小功能,可以我的博客查看博客地址

個人感覺mob平臺功能還是比較強大的,很多功能都可以通過他們平臺來實現(xiàn)。如果想要測驗源碼的,可以評論出你的QQ郵箱,或者私聊發(fā)給你。

建議仔細觀看每一個步驟,如果一個步驟沒處理好,可能就會讓你的這個功能無法實現(xiàn)。相信我一定可以成功的。
廢話少說,先看一下效果:

在這里插入圖片描述

我們想了解mob平臺更多實用的功能可以看一下我的其他博客:

android怎么實現(xiàn)手機號登錄(2020最新版超詳細Mob平臺+SMSSDK)?

android教你怎么輕松實現(xiàn)手機推送功能,一步一步教你Mob+MobPush實現(xiàn)推送功能?

1.在mob平臺配置ShareSDK環(huán)境

1.如何在mob平臺創(chuàng)建應用

上面的博客我已經(jīng)出過兩期如何在mob平臺創(chuàng)建應用,所以這里就不仔細說明了,如果不知道怎么在mob平臺創(chuàng)建應用的,可以去看一下我們上面的那些博客。mob平臺地址

下面為我創(chuàng)建的應用,如圖所示,我們選擇接入的接口為ShareSDK
在這里插入圖片描述

2.獲取你的App KeyApp Secret(建議用自己的)

獲取你先創(chuàng)建應用的App Key和App Secret,這里主要告訴你在哪里找App Key和App Secret,因為等下需要用到。

在這里插入圖片描述

3.點擊SDK下載

在這里插入圖片描述

4.選擇ShareSDK

在這里插入圖片描述

選擇配置我們主要選擇下方配置,然后點擊保存配置(下面我會說明為什么只選擇這些配置)。
在這里插入圖片描述

5.點擊下載,就會彈出下載提示

在這里插入圖片描述

6.關于為什么只選擇這些配置?

可以從上圖中看出,無論是QQ還微博,微信等都需要獲取他們平臺的appId和appKey如果你直接使用平臺的提供的appId和appKey是不能成功的。如果我想要實現(xiàn)這些功能,我們得先去那些平臺獲取他的appId和appKey。因為獲取過程太過于麻煩,我只獲取了QQ平臺的,下面是獲取所有平臺的鏈接地址。
獲取所有平臺appId的鏈接

在這里插入圖片描述

7.我創(chuàng)建的QQ開發(fā)者平臺應用

下面是我創(chuàng)建成功的應用,說實話QQ的這個服務確實挺慢的,一般審核需要幾天,可以是免費的原因吧。下面是創(chuàng)建應用的步驟。
1.成為個人開發(fā)者。
2.創(chuàng)建移動應用。

在這里插入圖片描述

2.把ShareSDK部署到android

1.新建一個moudle或者project

這個就不細說,比較簡單。

2.配置你的build.gradle(Project)文件

如圖所示,添加如圖代碼:


在這里插入圖片描述

代碼如下:

classpath "com.mob.sdk:MobSDK:2018.0319.1724"

3.配置你的build.gradle(Module)文件

1.首先加入如圖代碼

在這里插入圖片描述

根據(jù)你的情況選擇
代碼如下,因為我的版本是android studio 4.2最新版,加入方法如下:

id 'com.mob.sdk'

如果是其他android studio 3點多的版本,添加方法代碼:


apply plugin:'com.mob.sdk'

2.其次加入下圖代碼

在這里插入圖片描述

加入代碼如下(建議使用自己申請的appId和appKey,如果你使用的是我QQ申請的appId和appKey話,你是成功不了,關于為什么成功不了,我下面將會說明):


    MobSDK {
        fp true //嚴格模式
    }

    MobSDK {
        appKey "31c48ca47c70e"
        appSecret "2d7adbfcd73363bbbe41aeff60e41e4f"
        ShareSDK {
            loopShare true
            devInfo {
                SinaWeibo {
                    appKey "568898243"
                    appSecret "38a4f8204cc784f81f9f0daaf31e02e3"
                    callbackUri "http://www.sharesdk.cn"
                }
                QQ {
                    appId "101906011"
                    appKey "676d885e518445fed4d7d2341ff2d56f"
                }
                TencentWeibo {
                    appKey "801307650"
                    appSecret "ae36f4ee3946e1cbb98d6965b0b2ff5c"
                    callbackUri "http://www.sharesdk.cn"
                }
                QZone {
                    appId "100371282"
                    appKey "aed9b0303e3ed1e27bae87c33761161d"
                }
            }
        }
    }

3. 解釋一下關于為什么成功不了

下面我創(chuàng)建的應用

在這里插入圖片描述

我們可以看一下在QQ平臺創(chuàng)建的應用包名和應用簽名如下
在這里插入圖片描述

然后我們在把他和我創(chuàng)建的android項目來對比一下
1.我的包名和他相同

在這里插入圖片描述

2.我們可以看一下我們的MD5是否相同
第一步:如果查看你的項目的MD5
找到圖中的gradle
在這里插入圖片描述

點擊圖中紅色圓圈處


在這里插入圖片描述

然后就可以查看MD5了


在這里插入圖片描述

第二步:將我的MD5的去掉':號',把大寫改為小寫,我們對比一下是相同的。

補充條件小彩蛋:==如果你新建其他項目android項目的時候需要用到QQ的appId和appKey的話,你可以直接點擊下圖中的修改,修改你為你新項目的包名和MD5==。


在這里插入圖片描述

即可直接進行修改


在這里插入圖片描述

4.配置你的AndroidManifest.xml文件

加入如圖回調(diào)和權限。

在這里插入圖片描述

在這里插入圖片描述

代碼如下:

權限:

    <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" />

回調(diào)代碼:

 <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"
     tools:ignore="LockedOrientationActivity">
     <!-- QQ和QQ空間分享 QQ登錄的回調(diào)必須要配置的 -->
     <intent-filter>
         <data android:scheme="tencent101906011" />
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.BROWSABLE" />
         <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>

     <!-- 新浪微博客戶端分享回調(diào)必須配置 -->
     <intent-filter>
         <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
         <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
 </activity>

注意事項
==記得把如圖紅色圓圈處的101906011改為你的QQ平臺創(chuàng)建的應用申請的appId==。

在這里插入圖片描述

5.配置activity.xml文件。

比較簡單,主要添加幾個控件用來接收數(shù)據(jù)


在這里插入圖片描述

代碼如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="名字"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="@+id/imageView"
        app:layout_constraintStart_toStartOf="@+id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/imageView"></TextView>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="340dp"
        android:text="QQ登錄"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.476"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="性別"
        app:layout_constraintEnd_toEndOf="@+id/text"
        app:layout_constraintStart_toStartOf="@+id/text"
        app:layout_constraintTop_toBottomOf="@+id/text" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="QQ分享"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

6.配置Activity.java文件

1.記得把MobSDK.init里面的AppKey和AppSecret改為你自己申請你的。

2.qq登錄代碼

Platform qq = ShareSDK.getPlatform(QQ.NAME);
qq.SSOSetting(false); // 設置false表示使用SSO授權方式
qq.authorize();

thirdLoginId = qq.getDb().getUserId();
name = qq.getDb().getUserName();
image = qq.getDb().getUserIcon();
sex = qq.getDb().getUserGender();
Toast.makeText(MainActivity.this,"name="+image,Toast.LENGTH_LONG).show();
TextView textView=findViewById(R.id.text);
textView.setText(name);
Log.e("Toast","image="+thirdLoginId);
ImageView imageView=findViewById(R.id.imageView);
Glide.with(MainActivity.this)
       .load(image)
       .into(imageView);
TextView textView1=findViewById(R.id.textView);
if(sex!=null){
   if(sex=="m"){
       sex= "男";
   }else {
       sex="女";
   }
}
textView1.setText(sex);

記得導入Glide依賴,因為獲取的圖片地址為網(wǎng)絡圖片地址

implementation 'com.github.bumptech.glide:glide:4.11.0'

3.qq分享代碼

 OnekeyShare oks = new OnekeyShare();
// title標題,微信、QQ和QQ空間等平臺使用
 oks.setTitle("分享");
// titleUrl QQ和QQ空間跳轉(zhuǎn)鏈接
 oks.setTitleUrl("http://sharesdk.cn");
// text是分享文本,所有平臺都需要這個字段
 oks.setText("我是分享文本");
// setImageUrl是網(wǎng)絡圖片的url
 oks.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
// url在微信、Facebook等平臺中使用
 oks.setUrl("http://sharesdk.cn");
// 啟動分享GUI
 oks.show(MobSDK.getContext());

4.總體代碼

package com.example.mobsharetest;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.mob.MobSDK;

import org.w3c.dom.Text;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private Button button2;
    String thirdLoginId;
    String name;
    String image;
    String sex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobSDK.init(this, "31c48ca47c70e","2d7adbfcd73363bbbe41aeff60e41e4f");
        MobSDK.submitPolicyGrantResult(true, null);
        button=findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Platform qq = ShareSDK.getPlatform(QQ.NAME);
                qq.SSOSetting(false); // 設置false表示使用SSO授權方式
                qq.authorize();
                
                thirdLoginId = qq.getDb().getUserId();
                name = qq.getDb().getUserName();
                image = qq.getDb().getUserIcon();
                sex = qq.getDb().getUserGender();
                Toast.makeText(MainActivity.this,"name="+image,Toast.LENGTH_LONG).show();
                TextView textView=findViewById(R.id.text);
                textView.setText(name);
                Log.e("Toast","image="+thirdLoginId);
                ImageView imageView=findViewById(R.id.imageView);
                Glide.with(MainActivity.this)
                        .load(image)
                        .into(imageView);
                TextView textView1=findViewById(R.id.textView);
                if(sex!=null){
                    if(sex=="m"){
                        sex= "男";
                    }else {
                        sex="女";
                    }
                }
                textView1.setText(sex);
            }
        });

        button2=findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnekeyShare oks = new OnekeyShare();
// title標題,微信、QQ和QQ空間等平臺使用
                oks.setTitle("分享");
// titleUrl QQ和QQ空間跳轉(zhuǎn)鏈接
                oks.setTitleUrl("http://sharesdk.cn");
// text是分享文本,所有平臺都需要這個字段
                oks.setText("我是分享文本");
// setImageUrl是網(wǎng)絡圖片的url
                oks.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
// url在微信、Facebook等平臺中使用
                oks.setUrl("http://sharesdk.cn");
// 啟動分享GUI
                oks.show(MobSDK.getContext());
            }
        });
    }

}

步驟有點復雜,因為這個功能確實比較難以實現(xiàn),如果有什么問題的話,可以提出來,一不小心就寫14000多個字了。

在這里插入圖片描述

因為這個也是我慢慢摸索過來,如果有更簡單的方法,可以提出來。

作者:android小白星
博客地址:https://blog.csdn.net/qq_45137584/article/details/113244389

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容