詳解android在mob平臺實(shí)現(xiàn)qq登陸和分享
個(gè)人感覺mob平臺功能還是比較強(qiáng)大的,很多功能都可以通過他們平臺來實(shí)現(xiàn)。
建議仔細(xì)觀看每一個(gè)步驟,如果一個(gè)步驟沒處理好,可能就會讓你的這個(gè)功能無法實(shí)現(xiàn)。相信我一定可以成功的。
廢話少說,先看一下效果:

1.在mob平臺配置ShareSDK環(huán)境
1.如何在mob平臺創(chuàng)建應(yīng)用
下面為我創(chuàng)建的應(yīng)用,如圖所示,我們選擇接入的接口為ShareSDK

2.獲取你的App Key和App Secret(建議用自己的)
獲取你先創(chuàng)建應(yīng)用的App Key和App Secret,這里主要告訴你在哪里找App Key和App Secret,因?yàn)榈认滦枰玫健?/p>

3.點(diǎn)擊SDK下載

4.選擇ShareSDK

選擇配置我們主要選擇下方配置,然后點(diǎn)擊保存配置(下面我會說明為什么只選擇這些配置)。

5.點(diǎn)擊下載,就會彈出下載提示

6.關(guān)于為什么只選擇這些配置?
可以從上圖中看出,無論是QQ還微博,微信等都需要獲取他們平臺的appId和appKey。如果你直接使用平臺的提供的appId和appKey是不能成功的。如果我想要實(shí)現(xiàn)這些功能,我們得先去那些平臺獲取他的appId和appKey。

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

2.把ShareSDK部署到android
1.新建一個(gè)moudle或者project
這個(gè)就不細(xì)說,比較簡單。
2.配置你的build.gradle(Project)文件
如圖所示,添加如圖代碼:

代碼如下:
classpath "com.mob.sdk:MobSDK:2018.0319.1724"
3.配置你的build.gradle(Module)文件
1.首先加入如圖代碼

根據(jù)你的情況選擇:
代碼如下,因?yàn)槲业陌姹臼莂ndroid studio 4.2最新版,加入方法如下:
id 'com.mob.sdk'
如果是其他android studio 3點(diǎn)多的版本,添加方法代碼:
apply plugin:'com.mob.sdk'
2.其次加入下圖代碼

加入代碼如下(建議使用自己申請的appId和appKey,如果你使用的是我QQ申請的appId和appKey話,你是成功不了,關(guān)于為什么成功不了,我下面將會說明):
MobSDK {
fp true //嚴(yán)格模式
}
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. 解釋一下關(guān)于為什么成功不了
下面我創(chuàng)建的應(yīng)用

我們可以看一下在QQ平臺創(chuàng)建的應(yīng)用包名和應(yīng)用簽名如下:

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

2.我們可以看一下我們的MD5是否相同
第一步:如果查看你的項(xiàng)目的MD5
找到圖中的gradle

點(diǎn)擊圖中紅色圓圈處

然后就可以查看MD5了

第二步:將我的MD5的去掉':號',把大寫改為小寫,我們對比一下是相同的。
補(bǔ)充條件小彩蛋:如果你新建其他項(xiàng)目android項(xiàng)目的時(shí)候需要用到QQ的appId和appKey的話,你可以直接點(diǎn)擊下圖中的修改,修改你為你新項(xiàng)目的包名和MD5。

即可直接進(jìn)行修改

4.配置你的AndroidManifest.xml文件
加入如圖回調(diào)和權(quán)限。


代碼如下:
權(quán)限:
<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"/>
<!-- 藍(lán)牙分享所需的權(quán)限 -->
<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>
注意事項(xiàng):
記得把如圖紅色圓圈處的101906011改為你的QQ平臺創(chuàng)建的應(yīng)用申請的appId。

5.配置activity.xml文件。
比較簡單,主要添加幾個(gè)控件用來接收數(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); // 設(shè)置false表示使用SSO授權(quán)方式
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);
記得導(dǎo)入Glide依賴,因?yàn)楂@取的圖片地址為網(wǎng)絡(luò)圖片地址
implementation 'com.github.bumptech.glide:glide:4.11.0'
3.qq分享代碼
OnekeyShare oks = new OnekeyShare();
// title標(biāo)題,微信、QQ和QQ空間等平臺使用
oks.setTitle("分享");
// titleUrl QQ和QQ空間跳轉(zhuǎn)鏈接
oks.setTitleUrl("http://sharesdk.cn");
// text是分享文本,所有平臺都需要這個(gè)字段
oks.setText("我是分享文本");
// setImageUrl是網(wǎng)絡(luò)圖片的url
oks.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
// url在微信、Facebook等平臺中使用
oks.setUrl("http://sharesdk.cn");
// 啟動(dòng)分享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); // 設(shè)置false表示使用SSO授權(quán)方式
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標(biāo)題,微信、QQ和QQ空間等平臺使用
oks.setTitle("分享");
// titleUrl QQ和QQ空間跳轉(zhuǎn)鏈接
oks.setTitleUrl("http://sharesdk.cn");
// text是分享文本,所有平臺都需要這個(gè)字段
oks.setText("我是分享文本");
// setImageUrl是網(wǎng)絡(luò)圖片的url
oks.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
// url在微信、Facebook等平臺中使用
oks.setUrl("http://sharesdk.cn");
// 啟動(dòng)分享GUI
oks.show(MobSDK.getContext());
}
});
}
}
步驟有點(diǎn)復(fù)雜,因?yàn)檫@個(gè)功能確實(shí)比較難以實(shí)現(xiàn),如果有什么問題的話,可以提出來,一不小心就寫14000多個(gè)字了。

以上就是詳解android在mob平臺實(shí)現(xiàn)qq登陸和分享的詳細(xì)內(nèi)容,更多關(guān)于android在mob平臺qq登陸和分享的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
仿餓了嗎點(diǎn)餐界面ListView聯(lián)動(dòng)的實(shí)現(xiàn)
這篇文章主要介紹了仿餓了嗎點(diǎn)餐界面ListView聯(lián)動(dòng)的實(shí)現(xiàn)的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android Listview中顯示不同的視圖布局詳解及實(shí)例代碼
這篇文章主要介紹了Android Listview中顯示不同的視圖布局詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android RecyclerView下拉刷新和上拉加載更多
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView下拉刷新和上拉加載更多,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
基于Flutter實(shí)現(xiàn)按位置大小比例布局的控件
做視頻監(jiān)控項(xiàng)目時(shí)需要需要展示多分屏,比如2x2、3x3、414等等,所以本文為大家介紹了如何基于Flutter實(shí)現(xiàn)按位置大小比例布局的控件,需要的可以參考一下2023-08-08
Android 自定義控件實(shí)現(xiàn)顯示文字的功能
這篇文章主要介紹了Android 自定義控件實(shí)現(xiàn)顯示文字的功能的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android簡單實(shí)現(xiàn)引導(dǎo)頁
這篇文章主要為大家詳細(xì)介紹了Android簡單實(shí)現(xiàn)引導(dǎo)頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Android中定時(shí)執(zhí)行任務(wù)的3種實(shí)現(xiàn)方法(推薦)
下面小編就為大家?guī)硪黄狝ndroid中定時(shí)執(zhí)行任務(wù)的3種實(shí)現(xiàn)方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
Android studio 連接手機(jī)調(diào)試操作步驟
這篇文章主要介紹了Android studio 連接手機(jī)調(diào)試操作步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

