android qq登录分析,Android实现QQ的第三方登录和分享

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

MainActivity.java

/**

* 实现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 platforms = new ArrayList();

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 data) {

//获取用户授权后的信息

Set 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

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/iv_login"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:src="@drawable/umeng_socialize_qq"/>

android:id="@+id/iv_share"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:src="@drawable/umeng_socialize_qzone"/>

android:id="@+id/tv_result"

android:text="ggg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

MyApp.java

public class MyAPP extends Application {

@Override

public void onCreate() {

super.onCreate();

//U盟SDK初始化

UMShareAPI.get(this);

}

{

PlatformConfig.setQQZone("1106036236","mjFCi0oxXZKZEWJs");

}

}

AndroidManifest.xml

xmlns:tools="http://schemas.android.com/tools"

package="com.sn.qqlogin">

tools:ignore="ProtectedPermissions"/>

android:name=".MyAPP"

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme">

android:name="UMENG_APPKEY"

android:value="573f0e9267e58e8e48001545">

android:name="com.umeng.qq.tencent.AuthActivity"

android:launchMode="singleTask"

android:noHistory="true">

android:name="com.umeng.qq.tencent.AssistActivity"

android:configChanges="orientation|keyboardHidden|screenSize"

android:screenOrientation="portrait"

android:theme="@android:style/Theme.Translucent.NoTitleBar"/>

build.gradle

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')

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值