Android 我想在安装时注册我的应用程序?
问题Android 我想在安装时注册我的应用程序?,android,android-layout,Android,Android Layout,问题 将进行注册的活动将仅显示一次 注册后,控件应移动到 下一项主要活动 我使用了以下代码 下面的代码不符合我的要求吗 任何帮助都将不胜感激 注册活动中的代码 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor=prefs.edit(); editor.putBoolean("registration",
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("registration", true);
editor.commit();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean regComplete =prefs.getBoolean("registration", false);
SharedPreferences.Editor editor =prefs.edit();
editor.putBoolean("registration", false);
editor.commit();
if(regComplete)
{
startActivity(new Intent(this, SecureXActivity.class));
} else
{
startActivity(new Intent(this, LoginActivity.class));
}
主活动中的代码
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("registration", true);
editor.commit();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean regComplete =prefs.getBoolean("registration", false);
SharedPreferences.Editor editor =prefs.edit();
editor.putBoolean("registration", false);
editor.commit();
if(regComplete)
{
startActivity(new Intent(this, SecureXActivity.class));
} else
{
startActivity(new Intent(this, LoginActivity.class));
}
只需将注册码放入
SecureXActivity.class
并在setContentView()
之前检查注册情况,如果未完成注册,则启动LoginActivity
.class
注册完成后,在LoginActivity.class
中输入这些代码
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("registration", true);
editor.commit();
如果您使用这种方法,那么我认为您不需要主活动类
请记住,所有这些都是在应用程序首次运行时完成的,而不是在安装时完成的。只需将注册码放入
SecureXActivity.class
并在setContentView()
之前检查注册情况,如果未完成注册,则启动LoginActivity
.class
注册完成后,在LoginActivity.class
中输入这些代码
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("registration", true);
editor.commit();
如果您使用这种方法,那么我认为您不需要主活动类
请记住,所有这些都是在应用程序首次运行时完成的,而不是在安装时完成的。注册活动应如下所示:
public class RegistrationActivity extends Activity {
public static SharedPreferences pref;
public static final String PREFS_NAME = "MyPrefsFile";
public void onCreate(Bundle savedInstanceState) {
pref = getSharedPreferences(PREFS_NAME, 0);
boolean regComplete =prefs.getBoolean("registration", false);
if(regComplete){
//go to main class
}else{
//stay in the registration class
}
}
}
主要类别应为:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
RegistrationActivity.pref = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("registration", true);
// Commit the edits!
editor.commit();
}
}
注册活动应如下所示:
public class RegistrationActivity extends Activity {
public static SharedPreferences pref;
public static final String PREFS_NAME = "MyPrefsFile";
public void onCreate(Bundle savedInstanceState) {
pref = getSharedPreferences(PREFS_NAME, 0);
boolean regComplete =prefs.getBoolean("registration", false);
if(regComplete){
//go to main class
}else{
//stay in the registration class
}
}
}
主要类别应为:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
RegistrationActivity.pref = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("registration", true);
// Commit the edits!
editor.commit();
}
}
这应该行得通。主要问题(在OP的原始代码中)是,在主活动中,程序开始的地方(每次运行),注册设置为false。每次都是。这样,您的用户每次重新运行程序时都必须注册。@Jochem-In-code of OP.OP启动
LoginActivity
如果注册未完成,并且注册代码位于RegistrationActivity
中。是的,但也有代码更改MainActivity中的首选项这应该可以工作。主要问题(在OP的原始代码中)是,在主活动中,程序开始的地方(每次运行),注册设置为false。每次都是。这样,您的用户每次重新运行程序时都必须进行注册。@Jochem-In-code of OP.OP启动LoginActivity
,如果注册未完成,并且注册代码位于RegistrationActivity
。是的,但也有代码更改MainActivity中的首选项