Android获取用户名

Android基于Linux,所以它也是支持多用户的。
当需要获取当前正在运行的user id(int)时,有两种方式:

android.os.Process.myUid()/100000
/**
* Returns the identifier of this process’s uid. This is the kernel uid
* that the process is running under, which is the identity of its
* app-specific sandbox. It is different from {@link #myUserHandle} in that
* a uid identifies a specific app sandbox in a specific user.
*/
public static final int myUid() {
return Os.getuid();
}

这种方式是先获取到进程的uid后除以100000得到user id。

从上面android.os.Process.myUid()注解可以看到,android.os.Process.myUid()获取得到的uid是当前进程运行所在的uid,表示的是运行进程的kernel uid,是 its app-specific sandbox( 这个不太懂具体是什么意思)的身份标记。该uid与{@link #myUserHandle}中包含的uid不同,myUserHandle中包含的uid标识特定用户中的a specific app sandbox(特定应用sandbox), 此种情况也就是下面介绍的第二种情况。

ActivityManager.getCurrentUser()
/**
* Gets the userId of the current foreground user. Requires system permissions.
* @hide
*/
@SystemApi
@RequiresPermission(anyOf = {
“android.permission.INTERACT_ACROSS_USERS”,
“android.permission.INTERACT_ACROSS_USERS_FULL”
})
public static int getCurrentUser() {
UserInfo ui;
try {
ui = getService().getCurrentUser();
return ui != null ? ui.id : 0;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}

从注解来看,这个方法获取的是当前前台用户的user id。根据源码提示,要使用该系统API是需要在AndroidManifest.xml文件中申请上面两个权限中的任意一个的(但通过实践证明,就算申请了上面的权限也是不好使的,因为该方式是需要系统签名的)

如果是不涉及多用户切换,非系统级别应用推荐使用第一种方式来获取当前的user id。但是如果涉及到用户切换,而且需要获取到当前foreground user, 则使用第二种方式来获取user id。

public void onClick(View v) {
switch (v.getId()) {

        case R.id.register_button:
            startActivity(new Intent(this,RegisterActivity.class));
            finish();
            break;

        case R.id.login_button:
            String name=editText_username.getText().toString().trim();
            String password=editText_userpassword.getText().toString().trim();
            if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(password)) {
                ArrayList<User> data = DateBase.getAllData();
                boolean match = false;

                for (int i = 0; i < data.size(); i++) {
                    User user = data.get(i);
                    if (name.equals(user.getName()) && password.equals(user.getPassword())) {
                        match = true;
                        break;
                    } else {
                        match = false;
                    }
                }

                if (match) {
                    Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                    //保存到本地当前登录的用户名,以供其他页面使用
                    String now_name = editText_username.getText().toString().trim();
                    FileOutputStream fos_now_name = null;//声明一个字节流,是追加写的模式
                    try {
                        fos_now_name = openFileOutput("now_name", MODE_PRIVATE);//容易抛出异常
                        try {
                            fos_now_name.write(name.getBytes());//容易抛出异常
                            fos_now_name.flush();//也是写入
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            fos_now_name.close();//关闭字节流,容易抛出异常
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    //保存到本地当前登录的用户名,以供其他页面使用
                    Intent intent = new Intent(this, function_menu.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(this, "用户名或密码不正确,请重新输入", Toast.LENGTH_SHORT).show();
                }
            }
            else {
                Toast.makeText(this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show();
            }
            break;
    }
}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值