经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
详解android在mob平台实现qq登陆和分享
来源:jb51  时间:2021/4/12 19:00:04  对本文有异议

个人感觉mob平台功能还是比较强大的,很多功能都可以通过他们平台来实现。

建议仔细观看每一个步骤,如果一个步骤没处理好,可能就会让你的这个功能无法实现。相信我一定可以成功的
废话少说,先看一下效果:

1.在mob平台配置ShareSDK环境

1.如何在mob平台创建应用

下面为我创建的应用,如图所示,我们选择接入的接口为ShareSDK

2.获取你的App KeyApp Secret(建议用自己的)

获取你先创建应用的App Key和App Secret,这里主要告诉你在哪里找App Key和App Secret,因为等下需要用到。

3.点击SDK下载

4.选择ShareSDK

选择配置我们主要选择下方配置,然后点击保存配置(下面我会说明为什么只选择这些配置)

5.点击下载,就会弹出下载提示

6.关于为什么只选择这些配置?

可以从上图中看出,无论是QQ还微博,微信等都需要获取他们平台的appId和appKey如果你直接使用平台的提供的appId和appKey是不能成功的。如果我想要实现这些功能,我们得先去那些平台获取他的appId和appKey。

7.我创建的QQ开发者平台应用

下面是我创建成功的应用,说实话QQ的这个服务确实挺慢的,一般审核需要几天,可以是免费的原因吧。下面是创建应用的步骤。
1.成为个人开发者。
2.创建移动应用。

2.把ShareSDK部署到android

1.新建一个moudle或者project

这个就不细说,比较简单。

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

如图所示,添加如图代码:

代码如下:

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

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

1.首先加入如图代码

根据你的情况选择
代码如下,因为我的版本是android studio 4.2最新版,加入方法如下:

  1. id 'com.mob.sdk'

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

  1. apply plugin:'com.mob.sdk'

2.其次加入下图代码

加入代码如下(建议使用自己申请的appId和appKey,如果你使用的是我QQ申请的appId和appKey话,你是成功不了,关于为什么成功不了,我下面将会说明):

  1. MobSDK {
  2. fp true //严格模式
  3. }
  4.  
  5. MobSDK {
  6. appKey "31c48ca47c70e"
  7. appSecret "2d7adbfcd73363bbbe41aeff60e41e4f"
  8. ShareSDK {
  9. loopShare true
  10. devInfo {
  11. SinaWeibo {
  12. appKey "568898243"
  13. appSecret "38a4f8204cc784f81f9f0daaf31e02e3"
  14. callbackUri "http://www.sharesdk.cn"
  15. }
  16. QQ {
  17. appId "101906011"
  18. appKey "676d885e518445fed4d7d2341ff2d56f"
  19. }
  20. TencentWeibo {
  21. appKey "801307650"
  22. appSecret "ae36f4ee3946e1cbb98d6965b0b2ff5c"
  23. callbackUri "http://www.sharesdk.cn"
  24. }
  25. QZone {
  26. appId "100371282"
  27. appKey "aed9b0303e3ed1e27bae87c33761161d"
  28. }
  29. }
  30. }
  31. }

3. 解释一下关于为什么成功不了

下面我创建的应用

我们可以看一下在QQ平台创建的应用包名和应用签名如下

然后我们在把他和我创建的android项目来对比一下
1.我的包名和他相同

2.我们可以看一下我们的MD5是否相同
第一步:如果查看你的项目的MD5
找到图中的gradle

点击图中红色圆圈处

然后就可以查看MD5了

第二步:将我的MD5的去掉':号',把大写改为小写,我们对比一下是相同的。

补充条件小彩蛋:如果你新建其他项目android项目的时候需要用到QQ的appId和appKey的话,你可以直接点击下图中的修改,修改你为你新项目的包名和MD5

即可直接进行修改

4.配置你的AndroidManifest.xml文件

加入如图回调和权限

代码如下:

权限:

  1. <uses-permission android:name="android.permission.GET_TASKS" />
  2. <uses-permission android:name="android.permission.INTERNET" />
  3. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  4. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  5. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  6. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  7. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  8. <uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
  9. <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
  10. <!-- 蓝牙分享所需的权限 -->
  11. <uses-permission android:name="android.permission.BLUETOOTH" />
  12. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

回调代码:

  1. <activity
  2. android:name="com.mob.tools.MobUIShell"
  3. android:theme="@android:style/Theme.Translucent.NoTitleBar"
  4. android:configChanges="keyboardHidden|orientation|screenSize"
  5. android:screenOrientation="portrait"
  6. android:windowSoftInputMode="stateHidden|adjustResize"
  7. tools:ignore="LockedOrientationActivity">
  8. <!-- QQ和QQ空间分享 QQ登录的回调必须要配置的 -->
  9. <intent-filter>
  10. <data android:scheme="tencent101906011" />
  11. <action android:name="android.intent.action.VIEW" />
  12. <category android:name="android.intent.category.BROWSABLE" />
  13. <category android:name="android.intent.category.DEFAULT" />
  14. </intent-filter>
  15.  
  16. <!-- 新浪微博客户端分享回调必须配置 -->
  17. <intent-filter>
  18. <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
  19. <category android:name="android.intent.category.DEFAULT" />
  20. </intent-filter>
  21. </activity>

注意事项
记得把如图红色圆圈处的101906011改为你的QQ平台创建的应用申请的appId

5.配置activity.xml文件。

比较简单,主要添加几个控件用来接收数据

代码如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8.  
  9. <TextView
  10. android:id="@+id/text"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginTop="24dp"
  14. android:text="名字"
  15. android:textSize="20sp"
  16. app:layout_constraintEnd_toEndOf="@+id/imageView"
  17. app:layout_constraintStart_toStartOf="@+id/imageView"
  18. app:layout_constraintTop_toBottomOf="@+id/imageView"></TextView>
  19.  
  20. <Button
  21. android:id="@+id/button"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_marginTop="340dp"
  25. android:text="QQ登录"
  26. app:layout_constraintEnd_toEndOf="parent"
  27. app:layout_constraintHorizontal_bias="0.476"
  28. app:layout_constraintStart_toStartOf="parent"
  29. app:layout_constraintTop_toTopOf="parent" />
  30.  
  31. <ImageView
  32. android:id="@+id/imageView"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_marginTop="56dp"
  36. android:src="@mipmap/ic_launcher_round"
  37. app:layout_constraintEnd_toEndOf="parent"
  38. app:layout_constraintStart_toStartOf="parent"
  39. app:layout_constraintTop_toTopOf="parent" />
  40.  
  41. <TextView
  42. android:id="@+id/textView"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:layout_marginTop="24dp"
  46. android:text="性别"
  47. app:layout_constraintEnd_toEndOf="@+id/text"
  48. app:layout_constraintStart_toStartOf="@+id/text"
  49. app:layout_constraintTop_toBottomOf="@+id/text" />
  50.  
  51. <Button
  52. android:id="@+id/button2"
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:layout_marginTop="50dp"
  56. android:text="QQ分享"
  57. app:layout_constraintEnd_toEndOf="parent"
  58. app:layout_constraintStart_toStartOf="parent"
  59. app:layout_constraintTop_toBottomOf="@+id/button" />
  60. </androidx.constraintlayout.widget.ConstraintLayout>

6.配置Activity.java文件

1.记得把MobSDK.init里面的AppKey和AppSecret改为你自己申请你的。

2.qq登录代码

  1. Platform qq = ShareSDK.getPlatform(QQ.NAME);
  2. qq.SSOSetting(false); // 设置false表示使用SSO授权方式
  3. qq.authorize();
  4.  
  5. thirdLoginId = qq.getDb().getUserId();
  6. name = qq.getDb().getUserName();
  7. image = qq.getDb().getUserIcon();
  8. sex = qq.getDb().getUserGender();
  9. Toast.makeText(MainActivity.this,"name="+image,Toast.LENGTH_LONG).show();
  10. TextView textView=findViewById(R.id.text);
  11. textView.setText(name);
  12. Log.e("Toast","image="+thirdLoginId);
  13. ImageView imageView=findViewById(R.id.imageView);
  14. Glide.with(MainActivity.this)
  15. .load(image)
  16. .into(imageView);
  17. TextView textView1=findViewById(R.id.textView);
  18. if(sex!=null){
  19. if(sex=="m"){
  20. sex= "男";
  21. }else {
  22. sex="女";
  23. }
  24. }
  25. textView1.setText(sex);

记得导入Glide依赖,因为获取的图片地址为网络图片地址

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

3.qq分享代码

  1. OnekeyShare oks = new OnekeyShare();
  2. // title标题,微信、QQ和QQ空间等平台使用
  3. oks.setTitle("分享");
  4. // titleUrl QQ和QQ空间跳转链接
  5. oks.setTitleUrl("http://sharesdk.cn");
  6. // text是分享文本,所有平台都需要这个字段
  7. oks.setText("我是分享文本");
  8. // setImageUrl是网络图片的url
  9. oks.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
  10. // url在微信、Facebook等平台中使用
  11. oks.setUrl("http://sharesdk.cn");
  12. // 启动分享GUI
  13. oks.show(MobSDK.getContext());

4.总体代码

  1. package com.example.mobsharetest;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.ImageView;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. import com.bumptech.glide.Glide;
  15. import com.mob.MobSDK;
  16.  
  17. import org.w3c.dom.Text;
  18.  
  19. import java.util.HashMap;
  20.  
  21. import cn.sharesdk.framework.Platform;
  22. import cn.sharesdk.framework.PlatformActionListener;
  23. import cn.sharesdk.framework.ShareSDK;
  24. import cn.sharesdk.onekeyshare.OnekeyShare;
  25. import cn.sharesdk.sina.weibo.SinaWeibo;
  26. import cn.sharesdk.tencent.qq.QQ;
  27.  
  28. public class MainActivity extends AppCompatActivity {
  29. private Button button;
  30. private Button button2;
  31. String thirdLoginId;
  32. String name;
  33. String image;
  34. String sex;
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_main);
  39. MobSDK.init(this, "31c48ca47c70e","2d7adbfcd73363bbbe41aeff60e41e4f");
  40. MobSDK.submitPolicyGrantResult(true, null);
  41. button=findViewById(R.id.button);
  42. button.setOnClickListener(new View.OnClickListener() {
  43. @Override
  44. public void onClick(View view) {
  45. Platform qq = ShareSDK.getPlatform(QQ.NAME);
  46. qq.SSOSetting(false); // 设置false表示使用SSO授权方式
  47. qq.authorize();
  48. thirdLoginId = qq.getDb().getUserId();
  49. name = qq.getDb().getUserName();
  50. image = qq.getDb().getUserIcon();
  51. sex = qq.getDb().getUserGender();
  52. Toast.makeText(MainActivity.this,"name="+image,Toast.LENGTH_LONG).show();
  53. TextView textView=findViewById(R.id.text);
  54. textView.setText(name);
  55. Log.e("Toast","image="+thirdLoginId);
  56. ImageView imageView=findViewById(R.id.imageView);
  57. Glide.with(MainActivity.this)
  58. .load(image)
  59. .into(imageView);
  60. TextView textView1=findViewById(R.id.textView);
  61. if(sex!=null){
  62. if(sex=="m"){
  63. sex= "男";
  64. }else {
  65. sex="女";
  66. }
  67. }
  68. textView1.setText(sex);
  69. }
  70. });
  71.  
  72. button2=findViewById(R.id.button2);
  73. button2.setOnClickListener(new View.OnClickListener() {
  74. @Override
  75. public void onClick(View v) {
  76. OnekeyShare oks = new OnekeyShare();
  77. // title标题,微信、QQ和QQ空间等平台使用
  78. oks.setTitle("分享");
  79. // titleUrl QQ和QQ空间跳转链接
  80. oks.setTitleUrl("http://sharesdk.cn");
  81. // text是分享文本,所有平台都需要这个字段
  82. oks.setText("我是分享文本");
  83. // setImageUrl是网络图片的url
  84. oks.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
  85. // url在微信、Facebook等平台中使用
  86. oks.setUrl("http://sharesdk.cn");
  87. // 启动分享GUI
  88. oks.show(MobSDK.getContext());
  89. }
  90. });
  91. }
  92.  
  93. }

步骤有点复杂,因为这个功能确实比较难以实现,如果有什么问题的话,可以提出来,一不小心就写14000多个字了

以上就是详解android在mob平台实现qq登陆和分享的详细内容,更多关于android在mob平台qq登陆和分享的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号