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

Android using shared preferences to check on first run

您好,我正在尝试检测我的应用程序是否是首次打开。如果有,我需要 运行 一个 activity 并且一旦它第二次打开它就不会再显示了。

这是我的代码:

片段:

@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();
}

但它所做的只是启动 activity,当我再次启动它时,它冻结在一个白屏中,但检查它显示的日志,就像 else 语句不断地 运行 一遍又一遍.我是 Android 的新手,所以非常感谢您的帮助或建议

看起来你的 activity 正在循环,因为在你的 else 语句中,你告诉它重新启动 activity,它再次出现在 else 语句中,依此类推。

如果 editor.apply 不起作用,请尝试使用 editor.commit

editor.putBoolean("firstRun", false); 
editor.apply(); // Save all changed settings
editor.commit(); // Save all changed settings

试试这个方法:

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();
    }
}

否则你的首发 "MainActivity.class"。 为什么要从 MainActivity 的 oncreate 加载 MainActivity ? 它会形成一个循环。

从 else 案例中删除开头 activity。

@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();
}

朋友请试试这个

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);
}
}

**先Activity勾选**

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);
}
    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();
                    }
                }

试试这个代码。