Android单点登录的模拟实现

来源:互联网 发布:网络热销产品排行榜 编辑:程序博客网 时间:2024/06/05 04:10
介绍一个实现单点登录的方法:通过多个应用之间共享SharedPreference文件来获取相应的登录状态,从而实现单点登录的功能。

一、实现SharedPreference文件共享的条件

   1、要实现SharedPreference文件的共享,需要将被共享的应用的SharedPreference文件的模式设置为MODE_WORLD_READABLEMODE_WORLD_WRITEABLE
   2、实现SharedPreference文件共享,如果两应用想要共享数据,必须使用同一个sharedUserId(关于shareUserId的介绍在另一篇文章中会介绍)

二、共享数据时常见的错误

1、如果两个应用没有使用相同单点shareUserId而直接来在一个应用程序中读取另一个应用程序的SharedPreference文件会报如下错误:

W/SharedPreferencesImpl(3609): Attempt to read preferences file /data/data/com.example.loginproject/shared_prefs/sp.xml without permission

解决方案:在两个应用程序的配置文件中的manifest标签中设置相同的shareUserId。

2、安装应用时报错

Installation error: INSTALL_FAILED_UID_CHANGEDPlease check logcat output for more details.Launch canceled!

解决方案:这个原因是由于以前的版本没有在配置文件中设置shareUserId,而当前的版本设置了。需要将原来的版本卸载后再进行安装新的版本,这样就不会出现问题了。

3、获取到的数据不是最新的

现象:能够成功的拿到共享的数据,但是当共享的数据更新时,不能拿到更新后的数据。
解决方案:在要获取共享数据的应用中修改getsharedpreference("config",otherAppsContext.MODE_SORLD_READABLE)的权限MODE_SORLD_READ修改为 MODE_MULTI_PROCESS。
下边直接上代码:
注:代码中包含两个工程,LoginProject是用来登录和修改登录状态的工程,OtherProject是用来获取LoginProject工程中共享的SharedPreference的工程。
主要代码如下:
com.example.loginproject.MainActivity:
package com.example.loginproject;import android.content.SharedPreferences;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends ActionBarActivity implements OnClickListener {private EditText etUserId;private EditText etPwd;private Button btnLogin;private final String LOGIN_STATE = "login_state";private Button btnLoginState;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {etUserId = (EditText) findViewById(R.id.et_userid);etPwd = (EditText) findViewById(R.id.et_pwd);btnLogin = (Button) findViewById(R.id.btn_login);btnLoginState = (Button) findViewById(R.id.btn_getloinstate);btnLogin.setOnClickListener(this);btnLoginState.setOnClickListener(this);}private final int FLAG =3;@Overridepublic void onClick(View arg0) {switch (arg0.getId()) {case R.id.btn_login:String userId = etUserId.getText().toString().trim();String password = etPwd.getText().toString().trim();if("abc".equals(userId) && "123".equals(password)){//登录成功.SharedPreferences preferences = getSharedPreferences("sp", FLAG);preferences.edit().putBoolean(LOGIN_STATE, true).commit();Toast.makeText(this, "登录成功", 1).show();}else{//登录失败SharedPreferences preferences = getSharedPreferences("sp", FLAG);preferences.edit().putBoolean(LOGIN_STATE, false).commit();Toast.makeText(this, "您输入的用户名或密码错误", 1).show();}break;case R.id.btn_getloinstate:SharedPreferences preferences = getSharedPreferences("sp", FLAG);boolean loginState = preferences.getBoolean(LOGIN_STATE, false);Toast.makeText(this, "当前的登录状态为::" + loginState, 1).show();break;default:break;}}}

com.example.otherproject.MainActivity:
package com.example.otherproject;import android.content.Context;import android.content.SharedPreferences;import android.content.pm.PackageManager.NameNotFoundException;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.widget.Toast;public class MainActivity extends ActionBarActivity {private final String LOGIN_STATE = "login_state";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void getLoginState(View v) {try {Context otherContext = createPackageContext("com.example.loginproject",Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);SharedPreferences preferences = otherContext.getSharedPreferences("sp", otherContext.MODE_MULTI_PROCESS);boolean loginState = preferences.getBoolean(LOGIN_STATE, false);if(loginState){//登录成功Toast.makeText(this, "已登录", 1).show();}else{Toast.makeText(this, "未登录", 1).show();}} catch (NameNotFoundException e) {e.printStackTrace();}}}


LoginProject中AndroidManifest文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.loginproject"    android:sharedUserId="com.example"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.loginproject.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>    </application></manifest>


OtherProject中的AndroidMainfest文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.otherproject"    android:versionCode="1"    android:sharedUserId="com.example"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.otherproject.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>    </application></manifest>


如果想要获取详细代码,可以点击下边链接免费下载:

点击打开链接
                                             
0 2
原创粉丝点击