`
king_tt
  • 浏览: 2109606 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

QQ和新浪帐号授权登录

阅读更多
package p.wy.hc.activity;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import p.wy.hc.bean.SharedPreferencesForLogin;
import p.wy.hc.util.JsonUtils;
import p.wy.hc.util.UpdateManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import cn.sharesdk.framework.AbstractWeibo;
import cn.sharesdk.framework.WeiboActionListener;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;

/**
 * @author yangyu
 *	功能描述:获取用户资料
 * 
 * 启动页面时传递一个int类型的字段type,用于标记获取自己的资料(type = 0)还是别人的资料(type = 1)。
 * 如果尝试获取别人的资料,示例代码会获取不同平台Share SDK的官方帐号的资料。
 * 
 * 如果资料获取成功,会通过{@link ShowInforPage}展示
 */
public class MainActivity  extends Activity implements Callback, OnClickListener,WeiboActionListener {
	
	//定义标题栏布局对象
//	private TitleLayout llTitle;
	
	private Button sinaBt,qzoneBt,logonBt;
	
	private TextView user_phone,user_psw,user_reg;
	private Handler handler;
	
	Message message = null;  
	private static final int DISMISS_PROGRESS_DIALOG = 1;  
	private ProgressDialog progressDialog = null; 
	private Button nextBt;
	private String userName;
	private String userPsw;
	private String flag="false"; //true 为登录成功
	private SharedPreferencesForLogin spflusrinfo;
	private String url;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
			
		//初始化ShareSDK
		AbstractWeibo.initSDK(this);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		handler = new Handler(this);
		setContentView(R.layout.logon_main);
//		setContentView(R.layout.logonmain);
		spflusrinfo= new SharedPreferencesForLogin(MainActivity.this,"shared_prefsusrinfo"); //user logon type			
		initView();
		
		initData();
		
		// 检查软件更新
		UpdateManager manager = new UpdateManager(MainActivity.this);
		manager.checkUpdate();
		
		initUser();
	}
	//查看本地是否有用户信息,有的话直接跳转到主页
	public void initUser(){
		String userinfo=spflusrinfo.getValue("usrinfo");
		
	}
	/**
	 * 初始化组件
	 */
	private void initView(){
		//得到按钮对象
		/******
		sinaBt    = (Button) findViewById(R.id.sina_id);
		qzoneBt   = (Button) findViewById(R.id.qq_id);
		
		user_phone=(TextView)findViewById(R.id.user_phone_number);
		user_psw=(TextView)findViewById(R.id.user_phone_psw);
		user_reg=(TextView)findViewById(R.id.user_reg);
		logonBt=(Button)findViewById(R.id.logon_btn);
		*****/
		sinaBt    = (Button) findViewById(R.id.sina_logon);
		qzoneBt   = (Button) findViewById(R.id.qq_logon);
	
	}

	
	/**
	 * 初始化数据
	 */
	private void initData(){	
		//设置监听
		sinaBt.setOnClickListener(this);
		qzoneBt.setOnClickListener(this);
		
		/****
		user_phone.setOnClickListener(this);
		user_reg.setOnClickListener(this);
		logonBt.setOnClickListener(this);
		
		user_phone.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				user_phone.setBackgroundResource(R.drawable.aabb2);
				user_psw.setBackgroundResource(R.drawable.m);
			}
		});
		user_psw.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				user_psw.setBackgroundResource(R.drawable.aabb1);
				user_phone.setBackgroundResource(R.drawable.n);
				user_psw.setText("");
			}
		});
		*****/
		
	}
	
	/**
	 * 点击按钮获取授权用户的资料
	 */
	@Override
	public void onClick(View v) {
		String name = null;	
		switch (v.getId()) {
		case R.id.sina_logon:
			name = SinaWeibo.NAME;
			break;
		case R.id.qq_logon:
			name = QZone.NAME;
			break;
		/***************
		switch (v.getId()) {
		case R.id.sina_id:
			name = SinaWeibo.NAME;
			break;
		case R.id.qq_id:
			name = QZone.NAME;
			break;
			
		case R.id.user_phone_number:
			user_phone.setBackgroundResource(R.drawable.aabb2);
			user_psw.setBackgroundResource(R.drawable.m);
			user_phone.setText("");
			break;
//		case R.id.user_phone_psw:
//			user_psw.setBackgroundResource(R.drawable.aabb1);
//			user_phone.setBackgroundResource(R.drawable.n);
//			user_psw.setText("");
//			break;
		case R.id.user_reg:
			Intent intentreg = new Intent();
			intentreg.setClass(MainActivity.this, RegisterInfoActivity.class);
			startActivity(intentreg);
			break;
		case R.id.logon_btn:
			userName=user_phone.getText().toString();
			userPsw=user_psw.getText().toString();
			Toast.makeText(MainActivity.this, "登录",Toast.LENGTH_SHORT).show();
			/*****
			new Thread(runnablelogon).start();
			if(flag.equals("true")){
				//登录成功跳转
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, HealthCheckActivity.class);
				intent.putExtra("userName", userName);
				startActivity(intent);
			}else{
				//登录失败
				Toast.makeText(MainActivity.this, "登录失败",Toast.LENGTH_SHORT).show();
			}

			break;
			
			*****/
		}	
		
		if (name != null) {
			AbstractWeibo weibo = AbstractWeibo.getWeibo(this, name);
			weibo.setWeiboActionListener(this);
			String account = null;
			weibo.showUser(account);
			HealthCheckApplication hca=(HealthCheckApplication)this.getApplication();
			hca.weibo=weibo;
		}
	}

	/*****
	//登录验证
	private Runnable runnablelogon = new Runnable() {
		public void run() {
			Map<String, String> map = new HashMap<String, String>();
			map.put("username", userName);
			map.put("pwd", userPsw);
			map.put("dtype", "18");
			String result=HttpClientUtil.getResultData(CDefault.LOGON_URL, map);
			flag=FileUtil.getJSON(result, "authkey");  //true 为登录成功
			
		}
	};
	*****/
	public void onComplete(AbstractWeibo weibo, int action,HashMap<String, Object> res) {
		Message msg = new Message();
		msg.arg1 = 1;
		msg.arg2 = action;
		msg.obj = weibo;
		handler.sendMessage(msg);
		
		Message msg2 = new Message();
		if(weibo.getDb().getWeiboNname().equals("QZone")){
			msg2.what = 2;
		}else if(weibo.getDb().getWeiboNname().equals("SinaWeibo")){
			msg2.what = 1;
		}
		JsonUtils ju = new JsonUtils();
		String json = ju.fromHashMap(res);
		msg2.obj = ju.format(json);
		handler.sendMessage(msg2);
	}

	public void onError(AbstractWeibo weibo, int action, Throwable t) {
		t.printStackTrace();
		
		Message msg = new Message();
		msg.arg1 = 2;
		msg.arg2 = action;
		msg.obj = weibo;
		handler.sendMessage(msg);
	}

	public void onCancel(AbstractWeibo weibo, int action) {
		Message msg = new Message();
		msg.arg1 = 3;
		msg.arg2 = action;
		msg.obj = weibo;
		handler.sendMessage(msg);
	}
	
	/** 处理操作结果 */
	public boolean handleMessage(Message msg) {
		
		switch(msg.what) {
			case 1: {
				try {
					SharedPreferencesForLogin spflsina = new SharedPreferencesForLogin(this,"shared_prefssina");
					String userdata=spflsina.getValue("strsina");
					if(userdata!=null){
						Intent intent = new Intent();
						intent.setClass(MainActivity.this, HealthCheckActivity.class);
						String[] userInfo=userdata.split(",");
						intent.putExtra("userName", userInfo[0]);
						intent.putExtra("userIamage", userInfo[1]);
						url=userInfo[1];
						new ShowImgThread().start();
						//intent.putExtra("userIamage", userImg);
						intent.putExtra("weiboData", "SinaWeibo");
						spflusrinfo.putValue("Logon_Sina", "SinaWeibo");
						spflusrinfo.putValue("Logon_QQ", null);
						startActivity(intent);
					}else{
						Intent i = new Intent(this, ShowInforActivity.class);
						i.putExtra("data", String.valueOf(msg.obj));
						i.putExtra("weiboData", "SinaWeibo");
						spflusrinfo.putValue("Logon_Sina", "SinaWeibo");
						spflusrinfo.putValue("Logon_QQ", null);
						startActivity(i);
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
			break;
			case 2: {
				try {
					SharedPreferencesForLogin spflqq = new SharedPreferencesForLogin(this,"shared_prefsqq");
					String userdata=spflqq.getValue("strqq");
					if(userdata!=null){
						System.out.println(userdata); //直接跳转
						Intent intent = new Intent();
						intent.setClass(MainActivity.this, HealthCheckActivity.class);
						String[] userInfo=userdata.split(",");
						intent.putExtra("userName", userInfo[0]);
						intent.putExtra("userIamage", userInfo[1]);
						new ShowImgThread().start();
						intent.putExtra("weiboData", "QZone");
						//intent.putExtra("userIamage", userImg);
						spflusrinfo.putValue("Logon_Sina", null);
						spflusrinfo.putValue("Logon_QQ", "QZone");
						startActivity(intent);
						url=userInfo[1];
						new ShowImgThread().start();
					}else{
						Intent i = new Intent(this, ShowInforActivity.class);
						i.putExtra("data", String.valueOf(msg.obj));
						i.putExtra("weiboData", "QZone");
						spflusrinfo.putValue("Logon_Sina", null);
						spflusrinfo.putValue("Logon_QQ", "QZone");
						startActivity(i);
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
			break;
			default: {
				AbstractWeibo weibo = (AbstractWeibo) msg.obj;
				String text = MainActivity.actionToString(msg.arg2);
				switch (msg.arg1) {
					case 1: { // 成功
						text = weibo.getName() + " completed at " + text;
					}
					break;
					case 2: { // 失败
						text = weibo.getName() + " caught error at " + text;
					}
					break;
					case 3: { // 取消
						text = weibo.getName() + " canceled at " + text;
					}
					break;
				}
				//Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
			}
			break;
		}
		return false;
	}

	/**
	 * 将action转换为String
	 */
	public static String actionToString(int action) {
		switch (action) {
			case AbstractWeibo.ACTION_AUTHORIZING: return "ACTION_AUTHORIZING";
			case AbstractWeibo.ACTION_GETTING_FRIEND_LIST: return "ACTION_GETTING_FRIEND_LIST";
			case AbstractWeibo.ACTION_FOLLOWING_USER: return "ACTION_FOLLOWING_USER";
			case AbstractWeibo.ACTION_SENDING_DIRECT_MESSAGE: return "ACTION_SENDING_DIRECT_MESSAGE";
			case AbstractWeibo.ACTION_TIMELINE: return "ACTION_TIMELINE";
			case AbstractWeibo.ACTION_USER_INFOR: return "ACTION_USER_INFOR";
			case AbstractWeibo.ACTION_SHARE: return "ACTION_SHARE";
			default: {
				return "UNKNOWN";
			}
		}
	}
	
	
	/**监听对话框里面的button点击事件*/  
    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()  
    {  
        public void onClick(DialogInterface dialog, int which)  
        {  
            switch (which)  
            {  
            case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序  
            	finish();  
            	  //这里处理退出前跳到首个加载界面后执行下面的退出
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, MainActivity.class);
				startActivity(intent);
				// 退出程序
				Intent mHomeIntent = new Intent(
						Intent.ACTION_MAIN);
				mHomeIntent.addCategory(Intent.CATEGORY_HOME);
				mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
				startActivity(mHomeIntent);
				// 第二次关闭
				android.os.Process
				.killProcess(android.os.Process.myPid());

                break;  
            case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框  
                break;  
            default:  
                break;  
            }  
        }  
    };    
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {  
        if (keyCode == KeyEvent.KEYCODE_BACK )  
        {  
            // 创建退出对话框  
            AlertDialog isExit = new AlertDialog.Builder(this).create();  
            // 设置对话框标题  
            isExit.setTitle("系统提示");  
            // 设置对话框消息  
            isExit.setMessage("确定要退出吗");  
            // 添加选择按钮并注册监听  
            isExit.setButton("确定", listener);  
            isExit.setButton2("取消", listener);  
            // 显示对话框  
            isExit.show();  
        }  
        return false;  
          
    }
    
    private class ShowImgThread extends Thread
    {
    	@Override
    	public void run()
    	{
    		Looper.prepare();

    		returnBitMap(url);

    		Looper.loop();
    	}

    };	
    //获取网络图片
    public void returnBitMap(String url) {   
    	URL myFileUrl = null;   
    	Bitmap bitmap = null;   
    	try {   
    		myFileUrl = new URL(url);   
    	} catch (MalformedURLException e) {   
    		e.printStackTrace();   
    	}   
    	try {   
    		if(myFileUrl!=null){
    			HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();   
    			conn.setDoInput(true);   
    			conn.connect();   
    			InputStream is = conn.getInputStream();   
    			bitmap = BitmapFactory.decodeStream(is);   
    			is.close();  
    		}
    	} catch (IOException e) {   
    		e.printStackTrace();   
    	}   
    	HealthCheckApplication applictaion=(HealthCheckApplication)this.getApplication();
    	
    	if(spflusrinfo.getValue("Logon_Sina")==null){
    		applictaion.mHeadBmQQ=bitmap;
    	}else{
    		applictaion.mHeadBmSina=bitmap;
    	}
//    	mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
    } 
}






源码下载地址 :http://www.32666.com/file-600256.html

  • 大小: 54.8 KB
分享到:
评论

相关推荐

    QQ,新浪,facebook账号登陆你的应用

    用facebook账号登陆你的应用的详细讲解,带源码

    Android实现第三方授权登录、分享以及获取用户资料

    由于公司项目的需要,要实现在项目中使用第三方授权登录以及分享文字和图片等这样的效果,几经波折,查阅了一番资料,做了一个Demo。实现起来的效果还是不错的,不敢独享,决定写一个总结的教程,供大家互相交流、...

    【第三方互联】十三、码云(Gitee)授权第三方登录

    今天我们来学习:码云(Gitee)授权第三方登录,相比之前 支付宝登录、腾讯QQ登录 以及 新浪微博登录 来说,相对于比较简单 一、准备工作 1、登录 码云官网 官网地址:https://gitee.com/ 注册、登录我们的账号 2、...

    【运营版】彩虹云7.27免授权版任务挂机平台/彩虹秒赞网平台PHP

    3.完善的QQ管理系统:增加QQ账号管理,添加QQ任务更加快捷,一键更新失效的sid。: 4.丰富的QQ挂机功能:拥有说说秒赞、秒评、自动图片说说、3G挂Q、QQ机器人等挂机功能。**留言、互赞主页 5.自动签到:包含柯林、DZ...

    ShareSDK超级强大的社会化分享.zip

    2、支持获取授权用户资料及其他用户资料,可以通过SDK制作使用新浪微博登录、QQ登录等 3、支持新浪微博SSO授权,省去用户输入密码,更方便安全 4、支持关注官方微博 5、支持关注微信公众帐号 6、支持一键...

    安卓项目ShareSDK超级强大的社会化分享(强大分享界面UI).zip

    2、支持获取授权用户资料及其他用户资料,可以通过SDK制作使用新浪微博登录、QQ登录等 3、支持新浪微博SSO授权,省去用户输入密码,更方便安全 4、支持关注官方微博 5、支持关注微信公众帐号 6、支持一键分享,...

    微小宝多平台运营助手 v2.3.0官方版.zip

    微小宝多平台运营助手是微小宝开发的公众号平台管理软件,是一款一站式内容创作、发布、互动和数据分析等管理神器,支持微信公众号平台、QQ公众平台、头条号、新浪微博等多个公众平台管理,提高工作效率。...

    启科网络PHP商城系统 v1.0.rar

    网站系统集商品、文章功能与一体,并且可以在后台自由定义商品、文章内容显示的链接及页面中的标题、关键字和描述,对于网站的搜索引擎优化非常有效。  另一方面,通过自由定义链接,还可以让访问者完全找不到网站...

    ShareSDK超级强大的社会化分享

    2、支持获取授权用户资料及其他用户资料,可以通过SDK制作使用新浪微博登录、QQ登录等 3、支持新浪微博SSO授权,省去用户输入密码,更方便安全 4、支持关注官方微博 5、支持关注微信公众帐号 6、支持一键...

    MaoBo微博安卓客户端开源(原名YiBo).zip

    5. 支持新浪、腾讯、搜狐、网易和Twitter五大微博平台,方便地消息同步和帐号管理;6. 支持3G、WIFI、CMNET和CMWAP等各种网络类型接入;7. 支持OAuth认证方式,保护帐号和通信安全;最新版本1、修复保存图片双份的bug...

    react-native-social-kit:对第三方社交账号SDK的封装,使开发者能在React Native App里使用授权、分享等功能

    目前仅支持授权功能,仅覆盖微信、新浪微博、QQ。 项目仍在开发中,功能不稳定,请耐心等待。 Demo 安装 npm install --save react-native-social-kit@latest 配置 iOS 需要使用来管理依赖。(诸如微信、QQ等第三方的...

    APP微博应用程序-最新微应用平台源码-微测试源码.rar

    最近微趣和微测试的网站趣火,大家有没有看到自己腾讯球球好友上或新浪微博的那些什么智商测试啊等等之类的信息?这就是微博测试的,只要一个好友测试了,其他好友看见了也会去测试,还有好友的好友。。。。。这样就...

    如何做好用户运营

    经过多年发展,互联网产品的注册渠道比较成熟,除了产品自身的注册入口外,还会有和一些站外...例如目前常见的第三方应用登录页面,提示可用新浪微博、QQ、MSN等账号直接登录,就是一种绑定授权的方式。这种方式可以

    盾灵自动流量CMS系统 v2.0.rar

    3、内容管理:在此进行管理发布的内容,可进行修改和删除操作 4、栏目管理:在此进行管理、添加、修改栏目信息 5、会员管理:可以根据账号或UID进行精确搜索和模糊搜索,在里面会员的信息一目了然,可以进行修改...

    威眼企业计算机监管系统 v3.6.11.zip

    禁用QQ、禁用MSN、禁用阿里旺旺、禁用新浪UC、禁用网易泡泡、禁用YY等聊天软件。禁用下载工具,如禁用迅雷、禁用QQ旋风等P2P下载工具。禁止员工上班时间玩游戏、禁用炒股软件、禁用视频软件等。 2、禁用USB存储设备...

    成都市安全服务目录(包括限价)

    对涉及到的相关信息进行在线和离线的安全取证,如即时通讯工具(QQ、MSN)、邮箱帐号(QQ邮箱、网易邮箱、Gmail邮箱)、微博帐号(腾讯微博、新浪微博)、论坛社区、网盘、操作系统等渠道安全取证支持 信息安全...

    汇通网盘资源搜索引擎官方版 v1.0

    8.本源码您可以免费使用,但是不能用于商业用途,如需用于商业用途,请购买商业授权(价格68元),未经商业授权,不得去除版权链接; 本源码的后台登录地址: http://您的域名/admin/tgzcm_Login.asp 默认管理员...

    司科仿美团O2O程序源码最新版+wap+微信版亲测完整版

    用户通过MSN 人人 网易 开心 豆瓣 百度 腾讯 飞信 新浪微博 QQ空间、等,快速分享给好朋友心怡的商品 网项目。 管理员等级及授权 总管理分配下属管理员,各自管理各自的项目 后台控制分类 自可以后台控制推荐产品...

Global site tag (gtag.js) - Google Analytics