我已经检查过我的应用程序是否第一次运行,如果应用程序第一次运行,它应该转到另一个名为Intro的活动,我使用了以下代码并且工作正常
SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(this);
if (!preferences.getBoolean("Time",false))
{
Intent intent=new Intent("com.hackerinside.jaisonjoseph.radioplanet.Intro");
startActivity(intent);
Toast.makeText(getApplicationContext(), "Welcome to Radio Planet ", Toast.LENGTH_LONG).show();
SharedPreferences.Editor editor=preferences.edit();
editor.putBoolean("Time",true);
editor.commit();
}
我担心的是当用户完成应用程序介绍时,它会进入我的主要活动或主屏幕,我也想在那里做一些事情。我怎么能这样做?
答案 0 :(得分:0)
从启动器活动中删除editor.putBoolean("Time",true);
并在检查后将其值设置为true检查主活动
答案 1 :(得分:0)
将此类创建为外部类。
public class SessionManager {
// LogCat tag
private static String TAG = SessionManager.class.getSimpleName();
// Shared Preferences
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "PREF_NAME";
private static final String KEY_IS_FIRSTTIME = "Time";
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void setFirst(boolean isFirst) {
editor.putBoolean(KEY_IS_FIRSTTIME, isFirst);
editor.commit(); // commit changes
Log.d(TAG, "User login session modified!");
}
public boolean getFirst() {
return pref.getBoolean(KEY_IS_FIRSTTIME, false);
}
}
然后在您的介绍活动中编写代码
if (new SessionManager(this).getFirst)
{
//write code not a first time
}else{
Intent intent=new Intent("com.hackerinside.jaisonjoseph.radioplanet.Intro");
startActivity(intent);
Toast.makeText(getApplicationContext(), "Welcome to Radio Planet ", Toast.LENGTH_LONG).show();
new SessionManager(this).setFirst(true);
}
with the same code you can check in mainActivity that it's first time or not.