建议下载demo,照着开发文档看
1.首先要配置SDK,官网下载的,两个jar。
2.配置AndroidManifest.xml 的权限和两个Activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iwtle.www.englishstudy">
<!--用户需要的权限-->
<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/studylogo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ui.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--需要配置的两个Activity-->
<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="Tencent101368807" />
<!--这个APPid需要替换成自己的-->
</intent-filter>
</activity>
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="behind"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
</application>
</manifest>
3.编写登录的代码。
3.1首先需要在onCreate初始化sdk
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTencent = Tencent.createInstance("101368807", this.getApplicationContext());
}
3.2创建回调的函数。
IUiListener loginListener = new BaseUiListener() {
@Override
protected void doComplete(JSONObject values) {
Log.d("SDKQQAgentPref", "AuthorSwitch_SDK:" + SystemClock.elapsedRealtime());
System.out.println("123456" + values.toString());
}
};
private class BaseUiListener implements IUiListener {
@Override
public void onComplete(Object response) {
if (null == response) {
return;
}
JSONObject jsonResponse = (JSONObject) response;
if (null != jsonResponse && jsonResponse.length() == 0) {
return;
}
doComplete((JSONObject) response);
System.out.println("123456789" + response.toString());
}
protected void doComplete(JSONObject values) {
}
@Override
public void onError(UiError e) {
System.out.println("12345678910" + e.errorDetail);
}
@Override
public void onCancel() {
System.out.println("12345678910" + "onCancel");
}
}
3.3还需要一个回调的方法(如果要成功接收到回调)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("12346", "-->onActivityResult " + requestCode + " resultCode=" + resultCode);
if (requestCode == Constants.REQUEST_LOGIN ||
requestCode == Constants.REQUEST_APPBAR) {
Tencent.onActivityResultData(requestCode, resultCode, data, loginListener);
}
super.onActivityResult(requestCode, resultCode, data);
}
3.4最后登录的代码
public void Login(View view) {
System.out.println("我要登录了!!");
if (!mTencent.isSessionValid()) {
mTencent.login(this, "all", loginListener);
}
}
4.登录页面的整体源码
public class MainActivity extends AppCompatActivity {
private Tencent mTencent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTencent = Tencent.createInstance("101368807", this.getApplicationContext());
}
public void Login(View view) {
System.out.println("我要登录了!!");
if (!mTencent.isSessionValid()) {
mTencent.login(this, "all", loginListener);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("12346", "-->onActivityResult " + requestCode + " resultCode=" + resultCode);
if (requestCode == Constants.REQUEST_LOGIN ||
requestCode == Constants.REQUEST_APPBAR) {
Tencent.onActivityResultData(requestCode, resultCode, data, loginListener);
}
super.onActivityResult(requestCode, resultCode, data);
}
IUiListener loginListener = new BaseUiListener() {
@Override
protected void doComplete(JSONObject values) { //获取数据
Log.d("SDKQQAgentPref", "AuthorSwitch_SDK:" + SystemClock.elapsedRealtime());
System.out.println("123456" + values.toString());
}
};
private class BaseUiListener implements IUiListener {
@Override
public void onComplete(Object response) { //登录成功获取数据
if (null == response) {
return;
}
JSONObject jsonResponse = (JSONObject) response;
if (null != jsonResponse && jsonResponse.length() == 0) {
return;
}
doComplete((JSONObject) response);
System.out.println("登录成功" + response.toString());
Toast.makeText(MainActivity.this, "登录成功" + response.toString(), Toast.LENGTH_LONG).show();
}
protected void doComplete(JSONObject values) {
}
@Override
public void onError(UiError e) {//登录出错
System.out.println("12345678910" + e.errorDetail);
}
@Override
public void onCancel() { //取消登录
System.out.println("12345678910" + "onCancel");
}
}
}
5.demo的网址。
链接:http://pan.baidu.com/s/1gfr6eDt 密码:2trk
我发现再好的教程,如果你只是搜到,而不去敲一遍。永远都不会去好好写的。