一,首先导入jar包
配置:
···
compile files('libs/open_sdk_r5990_lite.jar')
compile 'com.android.support:support-v4:26.0.0-alpha1'
···
在清单文件中添加以下权限,
···
···
添加activity标签
···
android:launchMode="singleTask"
android:noHistory="true" >
android:screenOrientation="portrait"/>
···
main.xml布局:
xml布局:
···
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lenovo.whkl.activity.LogoActivity">
android:layout_height="90dp"
android:background="@drawable/qq"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="161dp"
android:id="@+id/imageView2" />
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp" />
···
碎片中跳转到Activity:
···
package com.example.lenovo.whkl.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.lenovo.whkl.R;
import com.example.lenovo.whkl.activity.LogoActivity;
/**
- Created by lenovo on 2018/8/20.
*/
public class My_Fragment extends Fragment {
private TextView tx;
private ImageView img;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate( R.layout.my_fragment, null);
RelativeLayout logo = view.findViewById(R.id.rll_logo);
tx = view.findViewById(R.id.textView);
img = view.findViewById(R.id.imageView);
logo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(getActivity(),LogoActivity.class);
startActivityForResult(intent,1);
}
});
return view ;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==1 && resultCode==2){
Bundle extras = data.getExtras();
tx.setText(extras.getString("nickname"));
Glide.with(getActivity()).load(extras.getString("figureurl_2")).into(img);
}
}
}
···
登录Activity:
···
package com.example.lenovo.whkl.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.lenovo.whkl.R;
import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQToken;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import org.json.JSONException;
import org.json.JSONObject;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
public class LogoActivity extends AppCompatActivity {
private static final String TAG = "LogoActivity";
private static final String APP_ID = "1107401709";//官方获取的APPID
private Tencent mTencent;
private BaseUiListener mIUiListener;
private UserInfo mUserInfo;
String data="";
@InjectView(R.id.logo_1)
RelativeLayout logo1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logo);
ButterKnife.inject(this);
//传入参数APPID和全局Context上下文
mTencent = Tencent.createInstance(APP_ID,LogoActivity.this.getApplicationContext());
}
@OnClick(R.id.logo_1)
public void onViewClicked() {
/**通过这句代码,SDK实现了QQ的登录,这个方法有三个参数,第一个参数是context上下文,第二个参数SCOPO 是一个String类型的字符串,表示一些权限
官方文档中的说明:应用需要获得哪些API的权限,由“,”分隔。例如:SCOPE = “get_user_info,add_t”;所有权限用“all”
第三个参数,是一个事件监听器,IUiListener接口的实例,这里用的是该接口的实现类 */
mIUiListener = new BaseUiListener();
//all表示获取所有权限
Toast.makeText(this, "正在登录,请稍等。。。", Toast.LENGTH_SHORT).show();
mTencent.login(LogoActivity.this,"all", mIUiListener);
}
private class BaseUiListener implements IUiListener {
@Override
public void onComplete(Object response) {
Toast.makeText(LogoActivity.this, "授权成功", Toast.LENGTH_SHORT).show();
Log.e(TAG, "response:" + response);
data+="response:\n"+response;
JSONObject obj = (JSONObject) response;
try {
String openID = obj.getString("openid");
String accessToken = obj.getString("access_token");
String expires = obj.getString("expires_in");
mTencent.setOpenId(openID);
mTencent.setAccessToken(accessToken,expires);
QQToken qqToken = mTencent.getQQToken();
mUserInfo = new UserInfo(LogoActivity.this,qqToken);
mUserInfo.getUserInfo(new IUiListener() {
@Override
public void onComplete(Object response) {
Log.e(TAG,"登录成功"+response.toString());
data += "\nresponse:\n"+response;
try {
JSONObject jsonObject = new JSONObject(response.toString());
String nickname = jsonObject.getString("nickname");
String figureurl = jsonObject.getString("figureurl_2");
Intent intent = getIntent();
intent.putExtra("nickname",nickname);
intent.putExtra("figureurl_2",figureurl);
setResult(2,intent);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(UiError uiError) {
Log.e(TAG,"登录失败"+uiError.toString());
}
@Override
public void onCancel() {
Log.e(TAG,"登录取消");
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(UiError uiError) {
Toast.makeText(LogoActivity.this, "授权失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
Toast.makeText(LogoActivity.this, "授权取消", Toast.LENGTH_SHORT).show();
}
}
/**
* 在调用Login的Activity或者Fragment中重写onActivityResult方法
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == Constants.REQUEST_LOGIN){
Tencent.onActivityResultData(requestCode,resultCode,data,mIUiListener);
}
super.onActivityResult(requestCode, resultCode, data);
if (null != mTencent){
mTencent.onActivityResultData(requestCode,resultCode,data,mIUiListener);
}
}
}
···
ThreadMessage:
···
package com.example.lenovo.whkl.utils;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import java.lang.reflect.Field;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public final class ThreadManager {
// UI线程
private static Handler mManinHandler;
private static Object mMainHandlerLock = new Object();
// 本地改就行..不要传svn
public static final boolean DEBUG_THREAD = false;
/**
* AsyncTask的默认线程池Executor. 负责长时间的任务(网络访问) 默认3个线程
*/
public static final Executor NETWORK_EXECUTOR;
/**
* 副线程的Handle, 只有一个线程 可以执行比较快但不能在ui线程执行的操作. 文件读写不建议在此线程执行,
* 请使用FILE_THREAD_HANDLER 此线程禁止进行网络操作.如果需要进行网络操作. 请使用NETWORK_EXECUTOR
*/
private static Handler SUB_THREAD_HANDLER;
private static HandlerThread SUB_THREAD;
/**
* 文件读写线程的Handle, 只有一个线程 可以执行文件读写操作, 如图片解码等 此线程禁止进行网络操作.如果需要进行网络操作.
* 请使用NETWORK_EXECUTOR
*/
private static Handler FILE_THREAD_HANDLER;
/**
* 文件读写用的线程
*/
private static HandlerThread FILE_THREAD;
static {
NETWORK_EXECUTOR = initNetworkExecutor();
}
private static Executor initNetworkExecutor() {
Executor result = null;
// 3.0以上
if (Build.VERSION.SDK_INT >= 11) {
//result = AsyncTask.THREAD_POOL_EXECUTOR;
result = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
new LinkedBlockingQueue());
}
// 3.0以下, 反射获取
else {
Executor tmp = null;
try {
Field field = AsyncTask.class.getDeclaredField("sExecutor");
field.setAccessible(true);
tmp = (Executor) field.get(null);
} catch (Exception e) {
tmp = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
new LinkedBlockingQueue());
}
result = tmp;
}
if (result instanceof ThreadPoolExecutor) {
// core size减少为3个
((ThreadPoolExecutor) result).setCorePoolSize(3);
}
return result;
}
public static void init() {
}
/**
* 取得UI线程Handler
*
* @return
*/
public static Handler getMainHandler() {
if (mManinHandler == null) {
synchronized (mMainHandlerLock) {
if (mManinHandler == null) {
mManinHandler = new Handler(Looper.getMainLooper());
}
}
}
return mManinHandler;
}
/**
* 在网络线程上执行异步操作. 该线程池负责网络请求等操作 长时间的执行(如网络请求使用此方法执行) 当然也可以执行其他 线程和AsyncTask公用
*
* @param run
*/
public static void executeOnNetWorkThread(Runnable run) {
try {
NETWORK_EXECUTOR.execute(run);
} catch (RejectedExecutionException e) {
}
}
/**
* 获得文件线程的Handler.
* 副线程可以执行本地文件读写等比较快但不能在ui线程执行的操作.
* 此线程禁止进行网络操作.如果需要进行网络操作. 请使用NETWORK_EXECUTOR
*
* @return handler
*/
public static Handler getFileThreadHandler() {
if (FILE_THREAD_HANDLER == null) {
synchronized (ThreadManager.class) {
FILE_THREAD = new HandlerThread("QQ_FILE_RW");
FILE_THREAD.start();
FILE_THREAD_HANDLER = new Handler(FILE_THREAD.getLooper());
}
}
return FILE_THREAD_HANDLER;
}
public static Looper getFileThreadLooper() {
return getFileThreadHandler().getLooper();
}
public static Thread getSubThread() {
if (SUB_THREAD == null) {
getSubThreadHandler();
}
return SUB_THREAD;
}
/**
* 获得副线程的Handler.
* 副线程可以执行比较快但不能在ui线程执行的操作.
* 另外, 文件读写建议放到FileThread中执行 此线程禁止进行网络操作.如果需要进行网络操作.
* 请使用NETWORK_EXECUTOR
*
* @return handler
*/
public static Handler getSubThreadHandler() {
if (SUB_THREAD_HANDLER == null) {
synchronized (ThreadManager.class) {
SUB_THREAD = new HandlerThread("QQ_SUB");
SUB_THREAD.start();
SUB_THREAD_HANDLER = new Handler(SUB_THREAD.getLooper());
}
}
return SUB_THREAD_HANDLER;
}
public static Looper getSubThreadLooper() {
return getSubThreadHandler().getLooper();
}
/**
* 在副线程执行.
* 可以执行本地文件读写等比较快但不能在ui线程执行的操作.
* 此线程禁止进行网络操作.如果需要进行网络操作. 请使用NETWORK_EXECUTOR
*
* @return
*/
public static void executeOnSubThread(Runnable run) {
getSubThreadHandler().post(run);
}
}
···