您的位置:首页 > 其它

QQ登录

2016-06-02 08:37 323 查看
package com.example.qqlogin;

import org.json.JSONException;

import org.json.JSONObject;

import com.lidroid.xutils.BitmapUtils;

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 android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

private TextView tv;
private Tencent tencent;
private ImageView iv;
Handler handler=new Handler(){

@Override
public void handleMessage(Message msg) {
if(msg.what==1){
JSONObject response = (JSONObject) msg.obj;
String str;
try {
str = response.getString("nickname");
String url=response.getString("figureurl_1");
Toast.makeText(MainActivity.this, url, 0).show();
Intent intent=new Intent(MainActivity.this,TwoActivity.class);
intent.putExtra("st", str);
intent.putExtra("url", url);
startActivity(intent);

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv = (TextView) findViewById(R.id.tv);
iv = (ImageView) findViewById(R.id.iv);
}

    public void login(View v){

    String appid="1105364936";

   

    tencent = Tencent.createInstance(appid, getApplicationContext());

    tencent.login(this, "all", new MyIUiListener());

   

    }

      public class MyIUiListener implements IUiListener{

@Override
public void onCancel() {
// TODO Auto-generated method stub

}

@Override
public void onComplete(Object object) {
try {
String token = ((JSONObject) object).getString(Constants.PARAM_ACCESS_TOKEN);
String expires = ((JSONObject) object).getString(Constants.PARAM_EXPIRES_IN);
String openId = ((JSONObject) object).getString(Constants.PARAM_OPEN_ID);
tencent.setAccessToken(token, expires);
tencent.setOpenId(openId);
QQToken token1=tencent.getQQToken();
UserInfo info=new UserInfo(MainActivity.this, token1);
info.getUserInfo(new IUiListener() {

@Override
public void onError(UiError arg0) {
// TODO Auto-generated method stub

}

@Override
public void onComplete(Object arg0) {
handler.obtainMessage(1, arg0).sendToTarget();

}

@Override
public void onCancel() {
// TODO Auto-generated method stub

}
});

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
public void onError(UiError arg0) {
Toast.makeText(MainActivity.this, "登录失败", 0).show();

}

      }

  @Override

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  // TODO Auto-generated method stub

  if (requestCode == Constants.REQUEST_LOGIN) {

     Tencent.onActivityResultData(requestCode, resultCode, data, null);

     }

  super.onActivityResult(requestCode, resultCode, data);

  }
}

-------------------------------------登录成功

Intent intent=getIntent();
String str=intent.getStringExtra("st");
String url=intent.getStringExtra("url");
TextView tv=(TextView) findViewById(R.id.tv_two);
tv.setTextColor(Color.RED);
tv.setText(str);
wv = (WebView) findViewById(R.id.wv);
wv.setWebViewClient(new WebViewClient());

wv.loadUrl(url);

-----------------------权限

<uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

      <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="tencent1105364936" />

            </intent-filter>

        </activity>

        <activity

            android:name="com.tencent.connect.common.AssistActivity"

            android:screenOrientation="portrait"

            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: