服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现QQ的第三方登录和分享

Android实现QQ的第三方登录和分享

2022-08-27 16:12FanRQ_ Android

这篇文章主要为大家详细介绍了Android实现QQ的第三方登录和分享,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现QQ的第三方登录的具体代码,供大家参考,具体内容如下

MainActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
 * 实现QQ的第三方登录
 * 1.搭建环境
(添加Jar包,添加Res图片,布局,Values资源,添加权限,配置Activity信息,修改Key值,build签名配置,Application初始化)
 * 2.写布局
 * 3.登录的代码
 * 注意:必须用真机测试
 */
public class MainActivity extends AppCompatActivity {
 
 private ImageView iv_login;
 private TextView tv_result;
 
 //A.定义装平台的容器
 public ArrayList<SnsPlatform> platforms = new ArrayList<SnsPlatform>();
 private SHARE_MEDIA[] list = {SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE};
 private UMShareAPI mUMShareAPI;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  iv_login = (ImageView) findViewById(R.id.iv_login);
  tv_result = (TextView) findViewById(R.id.tv_result);
 
  //A.三方平台,添加到遍历的集合中
  initPlatforms();
 
  //A.获取UM的对象
  mUMShareAPI = UMShareAPI.get(MainActivity.this);
 
  //A.获取是否授权
  final boolean isauth = UMShareAPI.get(this).isAuthorize(this, platforms.get(0).mPlatform);
 
  //A.点击QQ的头像,进行授权
  iv_login.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
    if (isauth){
     Toast.makeText(MainActivity.this, "授权成功", Toast.LENGTH_SHORT).show();
     mUMShareAPI.deleteOauth(MainActivity.this, platforms.get(0).mPlatform,authListener);
    }else{
     mUMShareAPI.doOauthVerify(MainActivity.this, platforms.get(0).mPlatform,authListener);
    }
    mUMShareAPI.getPlatformInfo(MainActivity.this, platforms.get(0).mPlatform,authListener);
 
   }
  });
 
  //B.分享的逻辑代码
  ImageView iv_share = (ImageView) findViewById(R.id.iv_share);
 
  final UMImage image = new UMImage(MainActivity.this, "http://b.hiphotos.baidu.com/zhidao/pic/item/63d9f2d3572c11df28e42e30602762d0f703c2e8.jpg");//网络图片
  final UMImage imagelocal = new UMImage(this, R.mipmap.ic_launcher);
  imagelocal.setThumb(new UMImage(this, R.mipmap.ic_launcher));
  imagelocal.setTitle("易宸锋好帅");
  iv_share.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    new ShareAction(MainActivity.this).withMedia(image)
      .setPlatform(platforms.get(0).mPlatform)
      .setCallback(shareListener).share();
 
    new ShareAction(MainActivity.this).setPlatform(SHARE_MEDIA.QQ)
      .withText("hello")
      .setCallback(shareListener)
      .share();
   }
  });
 
 }
 
 //A.
 private void initPlatforms() {
  //A.集合清空
  platforms.clear();
  //A.通过for循环,把数组数据添加到集合中
  for (SHARE_MEDIA e : list) {
   if (!e.toString().equals(SHARE_MEDIA.GENERIC.toString())) {
    platforms.add(e.toSnsPlatform());
   }
  }
 }
 
 //A.
 UMAuthListener authListener = new UMAuthListener() {
  @Override
  public void onStart(SHARE_MEDIA platform) {
   //授权开始的回调,可以用来处理等待框,或相关的文字提示
  }
 
  @Override//授权成功时回调
  public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
   //获取用户授权后的信息
   Set<String> strings = data.keySet();
   data.get("profile_image_url");
   String temp="";
   for(String key: strings ){
    temp =temp +key +" :" +data.get(key) +"\n";
   }
   tv_result.setText(temp);
  }
 
  @Override
  public void onError(SHARE_MEDIA platform, int action, Throwable t) {
   Toast.makeText(MainActivity.this, "失败:" + t.getMessage(), Toast.LENGTH_LONG).show();
 
  }
 
  @Override
  public void onCancel(SHARE_MEDIA platform, int action) {
   Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_LONG).show();
  }
 };
 
 //A.
 @Override
 protected void onActivityResult ( int requestCode, int resultCode, Intent data){
  super.onActivityResult(requestCode, resultCode, data);
  UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
 }
 
 
 //B.分享的逻辑代码
 private UMShareListener shareListener = new UMShareListener() {
  @Override
  public void onStart(SHARE_MEDIA platform) {
  }
 
  @Override
  public void onResult(SHARE_MEDIA platform) {
   Toast.makeText(MainActivity.this, "成功了", Toast.LENGTH_LONG).show();
  }
 
  @Override
  public void onError(SHARE_MEDIA platform, Throwable t) {
   Toast.makeText(MainActivity.this, "失败" + t.getMessage(), Toast.LENGTH_LONG).show();
  }
 
  @Override
  public void onCancel(SHARE_MEDIA platform) {
   Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_LONG).show();
 
  }
 };
}

activity_main.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
 
  <ImageView
   android:id="@+id/iv_login"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:src="@drawable/umeng_socialize_qq"/>
 
  <ImageView
   android:id="@+id/iv_share"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:src="@drawable/umeng_socialize_qzone"/>
 </LinearLayout>
 
 <TextView
  android:id="@+id/tv_result"
  android:text="ggg"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 
</RelativeLayout>

MyApp.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class MyAPP extends Application {
 
 @Override
 public void onCreate() {
  super.onCreate();
  //U盟SDK初始化
  UMShareAPI.get(this);
 }
 
 {
  PlatformConfig.setQQZone("1106036236","mjFCi0oxXZKZEWJs");
 }
}

AndroidManifest.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   package="com.sn.qqlogin">
 
 <!--友盟所用的权限-->
 <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.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.READ_LOGS"
      tools:ignore="ProtectedPermissions"/>
 <uses-permission android:name="android.permission.CALL_PHONE"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission android:name="android.permission.GET_TASKS"/>
 
 <application
  android:name=".MyAPP"
  android:allowBackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:supportsRtl="true"
  android:theme="@style/AppTheme">
  <activity android:name=".MainActivity">
   <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
 
    <category android:name="android.intent.category.LAUNCHER"/>
   </intent-filter>
  </activity>
 
  <!--配置友盟上你应用注册的Key值,替换value-->
  <meta-data
   android:name="UMENG_APPKEY"
   android:value="573f0e9267e58e8e48001545">
  </meta-data>
 
  <!-- 友盟所需配置的Activity信息-->
  <!--注意:在自定义Application中的keyID必须要和清单文件的AuthActivity下的scheme="tencent???"保持一致-->
  <activity
   android:name="com.umeng.qq.tencent.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="tencent1106036236"/>
   </intent-filter>
  </activity>
  <activity
   android:name="com.umeng.qq.tencent.AssistActivity"
   android:configChanges="orientation|keyboardHidden|screenSize"
   android:screenOrientation="portrait"
   android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
 </application>
</manifest>

build.gradle

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
signingConfigs {
  debug {
   storeFile file('debug.keystore')
   storePassword "android"
   keyAlias "androiddebugkey"
   keyPassword "android"
  }
 }
 
compile files('libs/SocialSDK_QQ_Simplify.jar')
 compile files('libs/umeng_social_api.jar')
 compile files('libs/umeng_social_net.jar')
 compile files('libs/umeng_social_shareboard.jar')
 compile files('libs/umeng_social_tool.jar')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/FanRQ_/article/details/84109378

延伸 · 阅读

精彩推荐