移动开发----第三方QQ登录

package com.yzw.news.activity;


import java.util.List;
import java.util.Map;


import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yzw.news.R;
import com.yzw.news.bean.User;
import com.yzw.news.dao.UserDao;


public class LoginActivity extends BaseActivity {


/**
* 登陆界面
*/
private ImageView qq;
private UMShareAPI mShareAPI;
private String name;
private String url;
private String province;
private String gender;
private String city;
public static SharedPreferences sp;


@Override
public int getLayout() {
int activity_login = R.layout.activity_login;
return activity_login;
}


@Override
public void findView() {
qq = (ImageView) findViewById(R.id.qq);
tv_regist = (TextView) findViewById(R.id.tv_regist);
et_name = (EditText) findViewById(R.id.et_name);
et_pwd = (EditText) findViewById(R.id.et_pwd);
btn_log = (Button) findViewById(R.id.btn_log);
}


@Override
public void initData() {
mShareAPI = UMShareAPI.get(this);
sp = getSharedPreferences("info", MODE_PRIVATE);


}


@Override
public void setData() {
UserDao dao = new UserDao(this);
userList = dao.query();
}


@Override
public void setListener() {
qq.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View arg0) {
SHARE_MEDIA platform = SHARE_MEDIA.QQ;
mShareAPI.doOauthVerify(LoginActivity.this, platform,
umAuthListener);
}
});
tv_regist.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View arg0) {
Intent intent = new Intent(LoginActivity.this,
YanZhengActivity.class);
startActivity(intent);
}
});
btn_log.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View arg0) {
String username = et_name.getText().toString();
String password = et_pwd.getText().toString();
if (username == null || password == null||"".equals(username)||"".equals(password)) {
Toast.makeText(LoginActivity.this, "用户名或者密码不能为空!", 0)
.show();
} else {
for (User u : userList) {
if (u.getUsername().equals(username)
&& u.getPassword().equals(password)) {
Toast.makeText(LoginActivity.this, "登陆成功!", 0)
.show();
finish();
return;
} else {
Toast.makeText(LoginActivity.this, "用户名或者密码错误!", 0)
.show();
}


}
}


}
});
}


private UMAuthListener umAuthListener = new UMAuthListener() {
@Override
public void onComplete(SHARE_MEDIA platform, int action,
Map<String, String> data) {
Toast.makeText(LoginActivity.this, "Authorize OK",
Toast.LENGTH_SHORT).show();
// 鑾峰彇骞冲彴淇℃伅
mShareAPI.getPlatformInfo(LoginActivity.this, platform,
new UMAuthListener() {


@Override
public void onComplete(SHARE_MEDIA share_media, int i,
Map<String, String> map) {
// Set set = map.keySet();
name = map.get("screen_name");
url = map.get("profile_image_url");
province = map.get("province");
gender = map.get("gender");
city = map.get("city");
Toast.makeText(LoginActivity.this, "successs", 0)
.show();
sp.edit().putBoolean("islogin", true).commit();
Editor edit = sp.edit();
edit.putBoolean("islogin", true);
edit.putBoolean("isqq", true);
edit.putString("img", url);
edit.putString("name", name);
edit.commit();
finish();


// Iterator it = set.iterator();
// while (it.hasNext()) {
// Object o = it.next();
// // System.out.println(("key is :" + o
// + "-----value is :" + map.get(o)));


// }
}


@Override
public void onError(SHARE_MEDIA share_media, int i,
Throwable throwable) {


}


@Override
public void onCancel(SHARE_MEDIA share_media, int i) {


}
});
}


@Override
public void onError(SHARE_MEDIA platform, int action, Throwable t) {
Toast.makeText(LoginActivity.this, "Authorize fail",
Toast.LENGTH_SHORT).show();
}


@Override
public void onCancel(SHARE_MEDIA platform, int action) {
Toast.makeText(LoginActivity.this, "Authorize cancel",
Toast.LENGTH_SHORT).show();
}
};
private TextView tv_regist;
private EditText et_name;
private EditText et_pwd;
private Button btn_log;
private List<User> userList;


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mShareAPI.onActivityResult(requestCode, resultCode, data);


}
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhanwei0102/article/details/51563508
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭