Android实现QQ的第三方登录和分享
程序员文章站
2022-05-27 10:05:20
本文实例为大家分享了android实现qq的第三方登录的具体代码,供大家参考,具体内容如下
mainactivity.java
/**
* 实现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<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
<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
public class myapp extends application { @override public void oncreate() { super.oncreate(); //u盟sdk初始化 umshareapi.get(this); } { platformconfig.setqqzone("1106036236","mjfci0oxxzkzewjs"); } }
androidmanifest.xml
<?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
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')
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: Android登录代码MVP架构详解