Android使用共享首选项来检查首次运行

时间:2017-05-23 09:53:14

标签: java android android-fragments android-intent android-sharedpreferences

您好,我正在尝试检测我的应用是否第一次打开。如果有,我需要运行一个活动,一旦它第二次打开它就不应再显示它。

这是我的代码:

片段:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Intent intent = new Intent(getActivity(), TutorialFeaturedActivity.class);
    //startActivity(intent);

    SharedPreferences settings = this.getActivity().getSharedPreferences(PREFS_NAME, 0); // Get preferences file (0 = no option flags set)
    boolean firstRun = settings.getBoolean("firstRun", true); // Is it first run? If not specified, use "true"

    if(firstRun) {
        Log.w("onCreate: ","first time" );
        Intent intent = new Intent(getActivity(), TutorialFeaturedActivity.class);
        startActivity(intent);

        SharedPreferences.Editor editor = settings.edit(); // Open the editor for our settings
        editor.putBoolean("firstRun", false); // It is no longer the first run
        editor.apply(); // Save all changed settings
    } else {
        Log.w("onCreate: ","second time");
        Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
    }

    getSpecials();
}

但它所做的只是启动活动,当我再次启动它时,它会在白色屏幕中冻结,但检查它显示的日志,就像else语句一直在反复运行一样。我对Android很新,所以非常感谢一些帮助或建议

7 个答案:

答案 0 :(得分:1)

看起来您的活动正在循环,因为在您的else语句中,您告诉它重新启动在else语句中再次登陆的活动,依此类推等等。

答案 1 :(得分:1)

@Override
public void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);
    SharedPreferences pref = YourActivityName.this.getSharedPreferences(PREFS_NAME,0);
    SharedPreferences.Editor editor= pref.edit();
    boolean firstRun = pref.getBoolean("firstRun", true); 
    if(firstRun)
    {
        Log.i("onCreate: ","first time" );
        editor.putBoolean("firstRun",false);
        editor.commit();
        Intent intent = new Intent(getActivity(), TutorialFeaturedActivity.class);
        startActivity(intent);
    }
    else
    {
        Log.i("onCreate: ","second time");
        Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);

    }
   // getSpecials();
}

答案 2 :(得分:0)

如果editor.commit无效

,请尝试使用editor.apply
editor.putBoolean("firstRun", false); 
editor.apply(); // Save all changed settings
editor.commit(); // Save all changed settings

答案 3 :(得分:0)

尝试这种方法:

public class MainActivity extends Activity {

    SharedPreferences prefs = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (prefs.getBoolean("firstRun", true)) {
            Intent intent = new Intent(MainActivity.this, TutorialFeaturedActivity.class);
            startActivity(intent);
            prefs.edit().putBoolean("firstRun", false).commit();
        }
       else
        {
          //do nothing
        }

      getSpecials();
    }
}

答案 4 :(得分:0)

在其他情况下,您的首发“MainActivity.class”。 为什么从MainActivity的oncreate加载MainActivity? 它会循环。

从else案例中删除开始活动。

答案 5 :(得分:0)

请试试这个我的朋友

public class SessionManager {

private static String TAG = SessionManager.class.getSimpleName();

SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;

// Shared pref mode
int PRIVATE_MODE = 0;

// Shared preferences file name
private static final String PREF_NAME = "ManageRun";

private static final String KEY_IS_RUN = "isRun";


public SessionManager(Context context) {
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}

public void setLatest(boolean isRun) {

    editor.putBoolean(KEY_IS_RUN, isRun);
    // commit changes
    editor.commit();
    Log.d(TAG, "Manage Version session modified!");
}

public boolean isLatest() {
    return pref.getBoolean(KEY_IS_RUN, true);
}
}

**在第一次活动检查**

private SessionManager session;


session = new SessionManager(getApplicationContext());

if (session.isLatest()) {
    session.setLatest(false);
    Log.w("onCreate: ","first time" );
    Intent intent = new Intent(getActivity(), TutorialFeaturedActivity.class);
    startActivity(intent);
}
else
{
    Log.w("onCreate: ","second time");
    Intent intent = new Intent(getActivity(), MainActivity.class);
    startActivity(intent);
}

答案 6 :(得分:0)

    private boolean isFirstTime() {
        if (firstTime == null) {
            SharedPreferences mPreferences = this.getSharedPreferences("first_time", Context.MODE_PRIVATE);
            firstTime = mPreferences.getBoolean("firstTime", true);
            if (firstTime) {
                SharedPreferences.Editor editor = mPreferences.edit();
                editor.putBoolean("firstTime", false);
                editor.commit();
            }
        }
        return firstTime;
    }



 if (isFirstTime()) {

                    Intent i = new Intent(SplashActivity.this, Intro_slider.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                    finish();

                } else {
                    final UserFunctions uf = new UserFunctions();
                    if (uf.isUserLoggedIn(SplashActivity.this)) {
                        Intent i = new Intent(SplashActivity.this, MainActivity.class);
                        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);
                        finish();
                    } else {
                        Intent i = new Intent(SplashActivity.this, Social_Login_Activity.class);
                        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);
                        finish();
                    }
                }

试试这段代码。