关闭

关于Android Studio第三方登录(使用QQ登录)代码

标签: Androidandroid studio
2498人阅读 评论(4) 收藏 举报
分类:

关于Android Studio第三方登录(使用QQ登录)

新手上路,写的不好或者有错误之处请各位大神多多指教!!!

现在很多APP应用都实现了第三方登录:比如QQ登录、微信登录、微博登录等等。其实实现起来非常简单,那么废话就不多说了,接下来我们看看如何实现第三方登录。

1.首先我们需要到腾讯开发平台区申请一个开发者的账号,申请好了后登录。点击创建应用一步步往下就会获取到:APP ID:1105345875和APP KEY:********(很重要哦APP_ID)

连接地址:http://open.qq.com/进行注册或者登录。

2.QQ登录第三方SDK下载地址:

http://wiki.open.qq.com/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD

Android_API调用说明:

http://wiki.open.qq.com/index.php?title=Android_API%E8%B0%83%E7%94%A8%E8%AF%B4%E6%98%8E&=45038

3.点击下载Android版本的SDK。下载好后解压复制jar文件里面的mta-sdk-1.6.2.jar和open_sdk_r5509.jar放到AS项目中的libs文件夹下。然后选中这两个jar包,右键Add Library添加库,这样就可以调用SDK的代码了。

4.点击:AndroidManifest.xml文件,添加uses-permission如下代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jack.keep">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- SDK2.1新增获取用户位置信息 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<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.GET_TASKS" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_0"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".LinkUrlActivity"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ZhuCeActivity"
/>
<activity
android:name="com.example.jack.keep.DengLvActivity"
 />
<!--QQ SDK 必须写-->
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name="com.tencent.tauth.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="1105345875" />
</intent-filter>
</activity>
</application>

</manifest>

5.布局文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF666771"
android:orientation="vertical"
tools:context="com.example.jack.keep.DengLvActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">

<ImageView
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:src="@mipmap/return_up" />

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="登陆"
android:textColor="@color/white"
android:textSize="18sp" />
</RelativeLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:visibility="invisible" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/bianjibag"
android:orientation="horizontal">

<LinearLayout
android:id="@+id/ll01"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal">

<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:paddingLeft="2.5dp"
android:paddingRight="2.5dp"
android:src="@mipmap/ic_launcher" />

<TextView
android:layout_width="30dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="+86"
android:textSize="12sp" />
</LinearLayout>

<ImageView
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="@color/blue" />

<EditText
android:id="@+id/et01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:cursorVisible="true"
android:gravity="center|left"
android:hint="请输入手机就号码"
android:inputType="number"
android:singleLine="true" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="@drawable/bianjibag"
android:orientation="horizontal">

<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:paddingLeft="2.5dp"
android:paddingRight="2.5dp"
android:src="@mipmap/ic_launcher" />

<TextView
android:layout_width="30dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="密码"
android:textSize="12sp" />

<ImageView
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="@color/blue" />

<EditText
android:id="@+id/et02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:cursorVisible="true"
android:gravity="center|left"
android:hint="请输入手机就号码"
android:inputType="numberPassword"
android:singleLine="true" />


</LinearLayout>

<Button
android:id="@+id/denglv"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="@drawable/btnbg"
android:text="登陆"
android:textColor="@color/white" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_margin="10dp">

<TextView
android:id="@+id/zhuce"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="注册" />

<TextView
android:id="@+id/wangjimima"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center"
android:text="忘记密码?" />
</RelativeLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:orientation="horizontal">

<ImageView
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"
android:background="@color/blue" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="其他方式登陆"
android:textColor="@color/white"
android:textSize="14sp" />

<ImageView
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"
android:background="@color/blue" />
</LinearLayout>

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RadioButton
android:id="@+id/radio01"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="1"
android:background="@mipmap/share_qqbtn"
android:button="@null"
android:checked="false"
android:gravity="center" />

<RadioButton
android:id="@+id/radio02"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="1"
android:background="@mipmap/share_wechatbtn"
android:button="@null" />
</RadioGroup>

<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:layout_marginTop="20dp"
android:background="@color/blue" />

</LinearLayout>

6.登录界面代码:

public class DengLvActivity extends AppCompatActivity implements View.OnClickListener{
private EditText user;//用户名
private EditText pw;//密码
private RadioButton qqRadio;//QQ登录
private RadioButton weixingRadio;//微信登录
private Button startBtn;

private static final String TAG = DengLvActivity.class.getName();//获取当前的名字
//tencent sdk源码
private Tencent mTencent;
private IUiListener loginListener;
private IUiListener userInfoListener;
private String scope;
private UserInfo userInfo;
private final String APP_ID = "1105345875";// 测试时使用,真正发布的时候要换成自己的APP_ID

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//设置全屏
setContentView(R.layout.activity_deng_lv);
initviews();
initdatas();
}

//销毁QQ登录
@Override
protected void onDestroy() {
if (mTencent != null) {
mTencent.logout(DengLvActivity.this);
}
super.onDestroy();
}

/**
 * 初始化数据
 */
String userName;
String userPw;
private void initdatas() {
//获取用户名和密码
userName=user.getText().toString();
userPw=pw.getText().toString();
if(TextUtils.isEmpty(userName)&&TextUtils.isEmpty(userPw)){
Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
}else {
startActivity(new Intent(DengLvActivity.this, MainActivity.class));
}

//
mTencent = Tencent.createInstance(APP_ID, DengLvActivity.this);
//要所有权限,不用再次申请增量权限,这里不要设置成get_user_info,add_t
scope = "all";
loginListener = new IUiListener() {
@Override
public void onError(UiError arg0) {
// TODO Auto-generated method stub
}

@Override
public void onComplete(Object value) {
// TODO Auto-generated method stub
System.out.println("有数据返回..");
if (value == null) {
return;
}
try {
JSONObject jo = (JSONObject) value;

String msg = jo.getString("msg");

System.out.println("json=" + String.valueOf(jo));

System.out.println("msg="+msg);
if ("sucess".equals(msg)) {
Toast.makeText(DengLvActivity.this, "登录成功",
Toast.LENGTH_LONG).show();

String openID = jo.getString("openid");
String accessToken = jo.getString("access_token");
String expires = jo.getString("expires_in");
mTencent.setOpenId(openID);
mTencent.setAccessToken(accessToken, expires);
}
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
};
userInfoListener = new IUiListener() {
public void onError(UiError arg0) {
// TODO Auto-generated method stub
}
@Override
public void onComplete(Object arg0) {
// TODO Auto-generated method stub
if(arg0 == null){
return;
}
try {
JSONObject jo = (JSONObject) arg0;
int ret = jo.getInt("ret");
System.out.println("json=" + String.valueOf(jo));
if(ret == 100030){
//权限不够,需要增量授权
Runnable r = new Runnable() {
public void run() {
mTencent.reAuth(DengLvActivity.this, "all", new IUiListener() {
@Override
public void onError(UiError arg0) {
// TODO Auto-generated method stub
}
@Override
public void onComplete(Object arg0) {
// TODO Auto-generated method stub
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
});
}
};
DengLvActivity.this.runOnUiThread(r);
}else{
String nickName = jo.getString("nickname");
String gender = jo.getString("gender");

Toast.makeText(DengLvActivity.this, "你好," + nickName, Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
};

}
//qq登录方法
private void login() {
if (!mTencent.isSessionValid()) {
mTencent.login(DengLvActivity.this, scope, loginListener);
}
}
//在某些低端机上调用登录后,由于内存紧张导致APP被系统回收,登录成功后无法成功回传数据。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQUEST_API) {
if (resultCode == Constants.REQUEST_LOGIN) {
Tencent.handleResultData(data, loginListener);
}
super.onActivityResult(requestCode, resultCode, data);
}
}

/**
 * 初始化组件
 */
private void initviews() {
user= (EditText) findViewById(R.id.et01);
pw= (EditText) findViewById(R.id.et02);
startBtn= (Button) findViewById(R.id.denglv);
qqRadio = (RadioButton) findViewById(R.id.radio01);//qq登录按钮
//qqRadio进行监听
qqRadio.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
System.out.println("点击并使用qq登录按钮");
login();
}
});
}

}

代码到这里就完结了!!!运行OK效果图如下:

这里写图片描述

这里写图片描述

这里写图片描述

1
4

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3111次
    • 积分:60
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:2篇
    • 译文:1篇
    • 评论:4条
    文章分类
    文章存档
    最新评论