简单的三方登录SDK示例,Android Activity之间数据的传递
先建立Library工程,即普通工程然后在Android的属性勾选Library选项。
这里建立的工程为 mySdk ,Activity名为LoginActivity。
LoginActivity代码:
package com.example.mysdk; import android.os.Bundle;
import android.os.Debug;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; public class LoginActivity extends Activity implements OnClickListener { EditText usernameEditText,passwdEditText;
Button loginButton,calcelbButton;
TextView appidTextView; public Context mContext;
public String m_appId;
public Intent m_intent; public void Init(Context context,String appId)
{
mContext=context;
m_appId=appId;
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginlayout); appidTextView=(TextView)findViewById(R.id.appidTextView); usernameEditText=(EditText)findViewById(R.id.usernameTextEdit);
passwdEditText=(EditText)findViewById(R.id.passwdTextEidt); loginButton=(Button)findViewById(R.id.buttonLogin);
calcelbButton=(Button)findViewById(R.id.buttonCalcel); loginButton.setOnClickListener(this);
calcelbButton.setOnClickListener(this); m_intent=this.getIntent();
Bundle bundle=m_intent.getBundleExtra("bundle");
String appidString=bundle.getString("AppID");
judgeAppId(appidString); } @Override
public void onClick(View v)
{
int resId=v.getId();
if(resId==R.id.buttonLogin)
{
Log.d("mysdk", "buttonLogin Click");
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append("name:"+usernameEditText.getText());
stringBuffer.append("passwd:"+usernameEditText.getText()); Toast toast=Toast.makeText(this, stringBuffer.toString(), Toast.LENGTH_LONG);
toast.show(); Bundle resultBundle=new Bundle(); //判断帐号密码是否正确,正确就返回正确到前一个Activity
if("Misstea".equals(usernameEditText.getText().toString().trim()) && "Misstea".equals(passwdEditText.getText().toString().trim()))
{
Log.d("mysdk", "密码正确");
resultBundle.putInt("resultInt", 1); //密码正确,返回获取到的userId
resultBundle.putString("userId", "012312334234");
m_intent.putExtra("resultBundle", resultBundle); }
else
{
Log.d("mysdk", "密码错误");
resultBundle.putInt("resultInt", -1); //密码错误
m_intent.putExtra("resultBundle", resultBundle);
} this.setResult(RESULT_OK,m_intent);
this.finish();
}
else if(resId==R.id.buttonCalcel)
{
Log.d("mysdk", "buttonCalcel Click");
this.finish();
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} public void judgeAppId(String appId)
{
if(appId.equals("123456"))
{
Log.d("mysdk", "AppId is ok"+appId);
appidTextView.setText(appId);
}
else
{
Log.d("mysdk", "AppId is false"+appId);
appidTextView.setText("appid错误");
}
} }
mySDK Manifest文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mysdk"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mysdk.LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
loginlayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LoginActivity" > <EditText
android:id="@+id/usernameTextEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="68dp"
android:ems="10" /> <EditText
android:id="@+id/passwdTextEidt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/usernameTextEdit"
android:layout_below="@+id/usernameTextEdit"
android:layout_marginTop="34dp"
android:ems="10" > <requestFocus />
</EditText> <Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/passwdTextEidt"
android:layout_marginTop="38dp"
android:layout_toRightOf="@+id/textView2"
android:text="登录" /> <Button
android:id="@+id/buttonCalcel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/buttonLogin"
android:layout_alignBottom="@+id/buttonLogin"
android:layout_alignRight="@+id/passwdTextEidt"
android:layout_marginRight="20dp"
android:text="取消" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/passwdTextEidt"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/usernameTextEdit"
android:text="帐号:" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/passwdTextEidt"
android:layout_alignLeft="@+id/textView1"
android:layout_alignTop="@+id/passwdTextEidt"
android:text="密码:" /> <TextView
android:id="@+id/appidTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="" /> </RelativeLayout>
mySDK 登录界面的预览
好了,mySDK就写完了。
然后新建一个Android工程来调用mySDK这个Library。这里新建工程为 UseMySdk。
在UseMySdk 工程属性Android里面加入mySDK Library。
下面是代码已经界面文件。
MainActivity
package com.example.usemysdk; import android.os.Bundle;
import android.os.Debug;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; import com.example.mysdk.*; public class MainActivity extends Activity { Button LoginButton;
TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); textView=(TextView)findViewById(R.id.textView1); LoginButton=(Button)findViewById(R.id.button1);
LoginButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("UseMySdk", "LoginButton Click"); String AppId="123456";
Bundle bundle=new Bundle();
bundle.putString("AppID", AppId); Intent intent=new Intent();
intent.putExtra("bundle", bundle);
intent.setClassName(getApplication(), "com.example.mysdk.LoginActivity");
startActivityForResult(intent, 0); }
}); } @Override
protected void onActivityResult(int requestCode,int resultCode,Intent intent)
{
switch (resultCode)
{
case RESULT_OK:
Bundle bundle=intent.getBundleExtra("resultBundle");
int resultInt=bundle.getInt("resultInt"); if(resultInt==1)//登录成功
{
Log.d("UseMySdk", "登录成功!");
//获取userId
String userIdString=bundle.getString("userId");
textView.setText(userIdString);
}
else
{
Log.d("UseMySdk", "登录失败!");
}
break;
default:
break;
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="64dp"
android:text="Button" /> </RelativeLayout>
UseMySdk Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.usemysdk"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.usemysdk.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.mysdk.LoginActivity"></activity>
</application> </manifest>
在UseMySdk Manifest中有个很重要的:
一定要注册mysdk的Activity,不然程序会崩溃找不到需要start的Activity
<activity android:name="com.example.mysdk.LoginActivity"></activity>
界面预览图
三方平台登录流程:
1.打开程序,点击登录按钮,跳到三方平台SDK的登录界面
2.在三方平台界面登录后,返回登录结果,如果登录成功,还会返回三方平台的userID。
3.程序获取到了登录成功标志,以三方平台userID作为用户名登录程序。
简单的三方登录SDK示例,Android Activity之间数据的传递的更多相关文章
- android Activity之间数据传递 Parcelable和Serializable接口的使用
Activity之间传数据时,为了避免麻烦,往往会将一些值封装成对象,然后将整个对象传递过去.传对象的时候有两种情况,一种是实现Parcelable接口,一种是实现Serializable接口.0.解 ...
- Android activity之间数据传递和共享的方式之Application
1.基于消息的通信机制 Intent ---bundle ,extra 数据类型有限,比如遇到不可序列化的数据Bitmap,InputStream,或者LinkedList链表等等数据类型就不太好用 ...
- Android——不同activity之间数据传递
/* * 不同activity之间数据的传递 */ public class MainActivity extends Activity { private EditText et_name; @Ov ...
- 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)
1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- xamarin.android Activity之间跳转与传值
前言 由于需要,所以接触到这个新的安卓开发模式,我会把我的学习经历全都记录下来,希望对大家有用. 导读 关于Activity,学习过安卓的人也应该明白什么是Activity,推荐新手去看YZF的这篇文 ...
- 解析activity之间数据传递方法的详解
转自:http://www.jb51.net/article/37227.htm 本篇文章是对activity之间数据传递的方法进行了详细的分析介绍,需要的朋友参考下 1 基于消息的通信机制 ...
- Activity之间使用intent传递大量数据带来问题总结
转载:大飞 http://blog.csdn.net/rflyee/article/details/47441405 Activity之间使用Parcel传递大量数据产生的问题. Activity ...
- activity之间參数传递&&获取activity返回值&&activity生命周期
Activity之间參数传递 A activity想将參数传给B activity时能够利用Intent将消息带过去 Intent intent = new Intent(this,BActivity ...
- activity之间通过全局变量传递数据
activity之间通过全局变量传递数据 一.简介 Application域中的onCreate方法是Android程序的入口,Android程序运行的时候就自动加载Application的对象,感觉 ...
随机推荐
- ./ . 和#!/bin/bash 辨析Linux如何选择当前执行脚本的shell
最近遇到一个有趣的问题,让我搞清楚Linux如何选择当前执行脚本的shell 执行cts的的 media stress test 需要安装 android-cts-media-1.0.zip 把这个文 ...
- Milonga_百度百科
Milonga_百度百科 Milonga是Tango的一种.源于并盛行于阿根廷.6/8拍的舞曲.节奏为 AXX BXX CX 分别都是8分音符.由于第3组节奏音只有2个8分音符,比前2组而缺少 ...
- Study notes for Latent Dirichlet Allocation
1. Topic Models Topic models are based upon the idea that documents are mixtures of topics, where a ...
- Bootstrap3学习笔记
Bootstrap3简单介绍----专题1 声明:本文章是给初学bootstrap3的同学添加记忆的, 一定有非常多欠缺和不完好的地方, 希望能帮助到大家, 也希望能让很多其它的人了解boostrap ...
- Java学习——何为JNDI
曾记得在做机房收费系统的时候就接触到了API,由于它的功能非常强大,可是自己对它却不怎么了解.所以当时是又爱又怕.现在,一路走来才明确,事实上它就是一组接口.仅仅要我们去了解它就会发现.它事实上也没想 ...
- LintCode 二叉树的层次遍历 II
中等 二叉树的层次遍历 II 查看执行结果 42% 通过 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 您在真实的面试中是否遇到过这个 ...
- Android中View绘制优化之三---- 优化View
本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 译三: 优化视图 关于如何设计自定义View以及响应触摸时间等,请看Android developer : 地 ...
- Windows串口编程
串口基础知识 http://www.cnblogs.com/menlsh/archive/2013/01/28/2880580.html DTU知识 http://blog.csdn.net/xuto ...
- ExtJs4 笔记(12) Ext.toolbar.Toolbar 工具栏、Ext.toolbar.Paging 分页栏、Ext.ux.statusbar.StatusBar 状态栏
本篇讲解三个工具栏控件.其中Ext.toolbar.Toolbar可以用来放置一些工具类操控按钮和菜单,Ext.toolbar.Paging专门用来控制数据集的分页展示,Ext.ux.statusba ...
- Swift - 类型嵌套(以扑克牌结构体为例)
类型嵌套,简单来说实在一个类型中包含另外一个类型.我们拿一副扑克来说明. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 //类 ...