如何使用户登录从只有一台设备在同一时间 [英] How to make user login from only one device at a time

查看:219
本文介绍了如何使用户登录从只有一台设备在同一时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有打验证用户时RESTAPI。
这个API暴露于Android和iOS开发者,他们打这个API,允许用户登录到我们的应用程序

I have a RestAPI which when hit authenticate the user. This api is exposed to android and ios developers they hit this api to allow user to login to our app

我的要求是this.1)如果用户有手机说ABC手机IMEIABC1234。他从这款手机登录英寸现在,如果他试图比他不应该被允许从第二个电话登录到他的第二个电话登录。他应该首先从ABC设备注销。

My requirement is this.1) If user has cell phone say abc phone with IMEI "abc1234". He logs in from this phone. Now if he tries to log in from his 2nd phone than he should not be allowed to login from 2nd phone. He should first log out from abc device.

现在有并发症少
1)如果用户是从ABC记录。随着出了登录电子格式,他自己的手机或卸载应用程序。然后,我应该如何处理来自同一台设备或其他设备上登录。

now there is few complications 1) if user is logged from abc. With out loging out he formats his phone or uninstalls the app. Then how should I handle login from same device or other device.

基本上我想知道的策略或良好做法对这种类型的场景。

Basically I want to know about strategies or good practises for such type of scenarios.

推荐答案

使用共享preferences的解决方案,

Use SharedPreferences for solution,

有关如。

 public class Pref_Storage {
    private static SharedPreferences sharedPreferences = null;

    public static void openPref(Context context) {
        sharedPreferences = context.getSharedPreferences(context.getResources().getString(R.string.app_name),
                Context.MODE_PRIVATE);
    }

    public static void deleteKey(Context context, String key) {
        HashMap<String, String> result = new HashMap<String, String>();

        Pref_Storage.openPref(context);
        for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
                .entrySet()) {
            result.put(entry.getKey(), (String) entry.getValue());
        }

        boolean b = result.containsKey(key);
        if (b) {
            Pref_Storage.openPref(context);
            Editor prefsPrivateEditor = Pref_Storage.sharedPreferences.edit();
            prefsPrivateEditor.remove(key);

            prefsPrivateEditor.commit();
            prefsPrivateEditor = null;
            Pref_Storage.sharedPreferences = null;
        }
    }

    public static void setDetail(Context context, String key, String value) {
        Pref_Storage.openPref(context);
        Editor prefsPrivateEditor = Pref_Storage.sharedPreferences.edit();
        prefsPrivateEditor.putString(key, value);

        prefsPrivateEditor.commit();
        prefsPrivateEditor = null;
        Pref_Storage.sharedPreferences = null;
    }

    public static Boolean checkDetail(Context context, String key) {
        HashMap<String, String> result = new HashMap<String, String>();

        Pref_Storage.openPref(context);
        for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
                .entrySet()) {
            result.put(entry.getKey(), (String) entry.getValue());
        }

        boolean b = result.containsKey(key);
        return b;
    }

    public static String getDetail(Context context, String key) {
        HashMap<String, String> result = new HashMap<String, String>();

        Pref_Storage.openPref(context);
        for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
                .entrySet()) {
            result.put(entry.getKey(), (String) entry.getValue());
        }

        String b = result.get(key);
        return b;

    }
}

使用:

登录检查login_flag之前:

Before login check login_flag:

if (Pref_Storage.checkDetail(getApplicationContext(), "login_flag"))
{
    // Home Screen
}
else
{
    //Display Login Screen
}

在登录设置login_flag:

After Login set login_flag:

Pref_Storage.setDetail(getApplicationContext(), "login_flag", "0");

这篇关于如何使用户登录从只有一台设备在同一时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆