manifest代码,首先声明两个权限, 然后将sdk中jar包自带的两个activity写进来,然后在data标签里面填上你申请的appid(注意格式)
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.thirdlogin"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application 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> <activity android:name="com.tencent.tauth.AuthActivity" android:noHistory="true" android:launchMode="singleTask" > <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="tencent1105460914" /> </intent-filter> </activity> </application> </manifest>
Mainactivity代码如下:将申请到的APPID传到初始化传建函数的一个参数tencent=Tencent.createInstance("1105460914",getApplicationContext());
然后调用Tencent的login方法 使用了自己重新new的一个loginListener,在里面设置了一些登录状态
import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import com.tencent.connect.common.Constants; import com.tencent.tauth.IUiListener; import com.tencent.tauth.Tencent; import com.tencent.tauth.UiError; import org.json.JSONObject; public class MainActivity extends Activity { private Tencent tencent; private Button loginbtn; private boolean isServerSideLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loginbtn=(Button)findViewById(R.id.loginBtn); loginbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { tencent=Tencent.createInstance("1105460914",getApplicationContext()); if(!tencent.isSessionValid()) { tencent.login(MainActivity.this,"all",loginListener); } else { if(isServerSideLogin){ tencent.logout(MainActivity.this); isServerSideLogin=false; return; } tencent.logout(MainActivity.this); } } }); } IUiListener loginListener=new BaseUilListener(){ @Override protected void doComplete(JSONObject values){ initOpenidAndToken(values); } }; private class BaseUilListener 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); } protected void doComplete(JSONObject values){ } @Override public void onError(UiError uiError) { } @Override public void onCancel() { } } public void initOpenidAndToken(JSONObject jsonObject){ try{ String token=jsonObject.getString(Constants.PARAM_ACCESS_TOKEN); String expires=jsonObject.getString(Constants.PARAM_EXPIRES_IN); String openId=jsonObject.getString(Constants.PARAM_OPEN_ID); if(!TextUtils.isEmpty(token)&&!TextUtils.isEmpty(expires) && !TextUtils.isEmpty((openId))){ tencent.setAccessToken(token,expires); tencent.setOpenId(openId); } }catch (Exception e){ } } }