Android:程序跳过登录界面直接进入主界面(自动登录)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android:程序跳过登录界面直接进入主界面(自动登录)相关的知识,希望对你有一定的参考价值。
参考技术A public class WelcomeActivity extends Activity@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
/**
* 延迟3秒进入主界面
*/
new Handler().postDelayed(new Runnable()
@Override
public void run()
Intent intent=new Intent(WelcomeActivity.this,LoginActivity.class);
startActivity(intent);
WelcomeActivity.this.finish();
,1000*3);
public class LoginActivity extends Activity
SharedPreferences sprfMain;
SharedPreferences.Editor editorMain;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
//在加载布局文件前判断是否登陆过
sprfMain= PreferenceManager.getDefaultSharedPreferences(this);
editorMain=sprfMain.edit();
//.getBoolean("main",false);当找不到"main"所对应的键值是默认返回false
if(sprfMain.getBoolean("main",false))
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
LoginActivity.this.finish();
setContentView(R.layout.login);
login= (Button) findViewById(R.id.login);
//这里只是简单用按键模拟登录功能
login.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
editorMain.putBoolean("main",true);
editorMain.commit();
startActivity(intent);
LoginActivity.this.finish();
);
public class MainActivity extends AppCompatActivity
SharedPreferences sprfMain;
SharedPreferences.Editor editorMain;
Button exit;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exit= (Button) findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
//点击注销按键后调用LoginActivity提供的resetSprfMain()方法执行editorMain.putBoolean("main",false);,即将"main"对应的值修改为false
resetSprfMain();
Intent intent=new Intent(MainActivity.this,WelcomeActivity.class);
startActivity(intent);
MainActivity.this.finish();
);
public void resetSprfMain()
sprfMain= PreferenceManager.getDefaultSharedPreferences(this);
editorMain=sprfMain.edit();
editorMain.putBoolean("main",false);
editorMain.commit();
以上是关于Android:程序跳过登录界面直接进入主界面(自动登录)的主要内容,如果未能解决你的问题,请参考以下文章