Androidサードパーティ登録の腾讯QQ登録の実例コード
レイアウトファイル
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textSize="25sp"
android:layout_marginTop="100dp"
/>
リストファイルの配置
<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="tencent1106163416" /> <!-- APPID -->
</intent-filter>
</activity>
コード
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String APP_ID = "1106163416";// APPID
private Tencent mTencent;
private BaseUiListener mIUiListener;
private UserInfo mUserInfo;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// APPID Context
mTencent = Tencent.createInstance(APP_ID,MainActivity.this.getApplicationContext());
}
public void buttonLogin(View v){
/** ,SDK QQ , , context , SCOPO String ,
: API , “,” 。 :SCOPE = “get_user_info,add_t”; “all”
, ,IUiListener , */
mIUiListener = new BaseUiListener();
//all
mTencent.login(MainActivity.this,"all", mIUiListener);
}
/**
* IUiListener , 3
* onComplete onError onCancel
*/
private class BaseUiListener implements IUiListener {
@Override
public void onComplete(Object response) {
Toast.makeText(MainActivity.this, " ", Toast.LENGTH_SHORT).show();
Log.e(TAG, "response:" + 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(getApplicationContext(),qqToken);
mUserInfo.getUserInfo(new IUiListener() {
@Override
public void onComplete(Object response) {
Intent inten = new Intent(MainActivity.this,NextActivity.class);
startActivity(inten);
Log.e(TAG," "+response.toString());
}
@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(MainActivity.this, " ", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.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);
}
}
追加する権限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
以上は小编が绍介したAndroid第三者登录の腾讯QQ登录の実例コードです。皆さんに助けてほしいです。もし何か疑问がありましたら、メッセージをください。小编はすぐに返事します。ここでも私たちのサイトを応援してくれてありがとうございます。