先建立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;}@Overrideprotected 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);}@Overridepublic 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();//判断帐号密码是否正确,正确就返回正确到前一个Activityif("Misstea".equals(usernameEditText.getText().toString().trim()) && "Misstea".equals(passwdEditText.getText().toString().trim())){Log.d("mysdk", "密码正确");resultBundle.putInt("resultInt", 1); //密码正确,返回获取到的userIdresultBundle.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();}}@Overridepublic 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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubLog.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);}});}@Overrideprotected 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", "登录成功!");//获取userIdString userIdString=bundle.getString("userId");textView.setText(userIdString);}else{Log.d("UseMySdk", "登录失败!");}break;default:break;}}@Overridepublic 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中有个很重要的:

一定要注册mysdkActivity,不然程序会崩溃找不到需要start的Activity

<activity android:name="com.example.mysdk.LoginActivity"></activity>


界面预览图


三方平台登录流程:

1.打开程序,点击登录按钮,跳到三方平台SDK的登录界面

2.在三方平台界面登录后,返回登录结果,如果登录成功,还会返回三方平台的userID。

3.程序获取到了登录成功标志,以三方平台userID作为用户名登录程序。

更多相关文章

  1. android工程在eclipse中报错解决办法
  2. android键盘挡住界面
  3. Android(安卓)源码中编译Android(安卓)studio工程
  4. Smalidea+IntelliJ IDEA/Android(安卓)Studio动态调试安卓app教
  5. 基于Android(安卓)tess-two工程的数字识别
  6. linux android sdk update
  7. android应用去掉title bar 及全屏处理方法
  8. 谷歌开发工具Android(安卓)Studio安装使用图文教程
  9. android之访问被保护的资源

随机推荐

  1. Android 画椭圆
  2. 开发者必看|Android(安卓)8.0 新特性及开
  3. Maven Android Plugin
  4. android颜色代码
  5. 【Android】自定义权限
  6. Android 电量显示Widgets插件实现
  7. android FloatingActionButton
  8. android 调用摄像头
  9. Android(安卓)应用程序不能全屏显示
  10. Android版本名称、版本号、API level对应