运行Android应用程序时如何自动登录

编程入门 行业动态 更新时间:2024-10-10 09:15:53
本文介绍了运行Android应用程序时如何自动登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我已经使用PHP + MySQL登录到系统,然后在退出应用程序后再次运行应用程序,无需再次登录并自动登录,请帮助解决问题.

Assume that I have login to system using PHP + MySQL, then, after I exit the apps, I run the apps again, no need to login again and auto login, Please help to solve the problem.

推荐答案

在首次登录时,您会以响应方式获取用户名或用户名,而不是将用户名或用户名存储在sharedpreference中,如下所示

while first time login u get something username or userid in respones than store ther username or userid in sharedpreference like as shown below

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(YourActivity.this); prefs.edit().putInt("userid",userid)mit();

成功登录后,这将优先存储您的用户ID,您可以检查您是否已经登录

as after succesfull login this will store your userid in preference and u can check u are already login or not as

int Userid; prefs = PreferenceManager.getDefaultSharedPreferences(YourActivity.this); Userid= mPrefs.getInt("userid", 0); which will return defalut value 0 if u are not login

检查后

if(Userid>0){ // Means u are already logged in do here code what u want if u are login }else{ // Means u are not logged in than go to your login pageview from here }

这只是做您想要的事情的简单方法,如果您希望在单击按钮时比在onclick事件上需要注销功能,则必须添加以下代码以退出注销

this is just simple way for doing what u want suppose if u want logoout functionality on button click than onclick event u have to add following code for logout which will clear prefernce

prefs = PreferenceManager.getDefaultSharedPreferences(MyAccountActivity.this); prefs.edit().clear()mit();

更多推荐

运行Android应用程序时如何自动登录

本文发布于:2023-11-27 15:32:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   自动登录   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!