2011-08-30 90 views

回答

46

我通常所做的是在Main Activity中添加一个特定的共享首选项的检查:如果缺少该共享首选项,请启动单一运行的Activity,否则继续主要活动。当您启动单次运行时,创建共享首选项,以便下次跳过。

编辑:在我onResume默认的活动我这样做:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
    boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false); 
    if(!previouslyStarted) { 
     SharedPreferences.Editor edit = prefs.edit(); 
     edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE); 
     edit.commit(); 
     showHelp(); 
    } 

基本上我加载默认共享偏好,并寻找previously_started布尔偏好。如果尚未设置,我将其设置,然后启动帮助文件。我使用它在第一次安装应用程序时自动显示帮助。

+0

很好的建议!我会试试看!。你能提供一个小代码应该如何运作的例子吗? –

+0

那么布尔先前开始做什么? –

+0

那么,如果previousStarted isnt null会发生什么? –

11

这样的事情可能会起作用。

public class MyPreferences { 

    private static final String MY_PREFERENCES = "my_preferences"; 

    public static boolean isFirst(Context context){ 
     final SharedPreferences reader = context.getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE); 
     final boolean first = reader.getBoolean("is_first", true); 
     if(first){ 
      final SharedPreferences.Editor editor = reader.edit(); 
      editor.putBoolean("is_first", false); 
      editor.commit(); 
     } 
     return first; 
    } 

} 

使用

boolean isFirstTime = MyPreferences.isFirst(MyActivity.this); 
+0

因此,在onCreate()我可以测试IS_FIRST布尔值,如果它继续。如果没有,然后停止并提醒用户或我想做的任何事情? –

+1

当然你可以做到。 – Samuel

1

我做了这个没有共享Prefrence ......我所知共享prefrence占用一些内存,所以我用公共静态布尔变量在全局类....首先,我做全局类的AppConfig ......然后我做了布尔静态变量是这样的:

public class Appconfig { 

    public static boolean activity = false; 
} 

然后我用这个公共静态布尔变量到我的欢迎活动类。我正在使用许可证协议页面。我只能在我的应用程序中只使用一次,然后在运行应用程序时再也不显示。所以我已经把condtion在欢迎活动......如果欢迎类中运行第一次如此的静态布尔变量是假的......

if (Appconfig.activity == false) { 
Intent intent = new Intent(); 
intent.setClass(WelcomeActivity.this,LicesnceActivity.class); 
startActivity(intent); 
WelcomeActivity.this.finish(); 
} 
if (Appconfig.activity == true) { 

Intent intent = new Intent(); 
intent.setClass(WelcomeActivity.this, MainActivity.class); 
    startActivity(intent); 

} 

现在在Licesnce Activity类我做:

Appconfig.activity=true; 

所以每当我运行应用程序的第二个活动“主要业务”欢迎活动后不运行许可证活动....

+2

这也消耗从RAM(这是更关键),当应用程序被垃圾,这意味着该值将被设置为默认值,这是错误的,对吗? –

11

安置自己的onCreate语句中,下面的代码

Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE) 
      .getBoolean("isFirstRun", true); 

    if (isFirstRun) { 
     //show start activity 

     startActivity(new Intent(MainActivity.this, FirstLaunch.class)); 
     Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG) 
       .show(); 
    } 


     getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit() 
       .putBoolean("isFirstRun", false).commit(); 

更换FirstLaunch.class与你想的类推出

+0

如果我想清除此偏好的值,该怎么办?我可以在你的代码中做到这一点吗? – Dre

0

在全球

public int count=0 
    int tempInt = 0; 

宣布在你的onCreate功能过去这段代码在第一。

count = readSharedPreferenceInt("cntSP","cntKey"); 
    if(count==0){ 
     Intent intent = new Intent(); 
     intent.setClass(MainActivity.this, TemporaryActivity.class); 
     startActivity(intent); 
     count++; 
     writeSharedPreference(count,"cntSP","cntKey"); 
     } 

过去这两种方法以外的onCreate

//Read from Shared Preferance 
    public int readSharedPreferenceInt(String spName,String key){ 
    SharedPreferences sharedPreferences = getSharedPreferences(spName,Context.MODE_PRIVATE); 
    return tempInt = sharedPreferences.getInt(key, 0); 
    }  

    //write shared preferences in integer 
    public void writeSharedPreference(int ammount,String spName,String key){ 

    SharedPreferences sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 

    editor.putInt(key, ammount); 
    editor.commit(); 
} 
2
SharedPreferences dataSave = getSharedPreferences("firstLog", 0); 

if(dataSave.getString("firstTime", "").toString().equals("no")){ // first run is happened 
} 
else{ // this is the first run of application 
SharedPreferences.Editor editor = dataSave.edit(); 
       editor.putString("firstTime", "no"); 
       editor.commit(); 
} 
0

申报全局

public int count=0; 
    int tempInt = 0; 

第一屏幕的OnCreate中

count = readSharedPreferenceInt("cntSP","cntKey"); 
       if(count==0){ 
        Intent i = new Intent(SplashScreen.this, IntroActivity.class); 
        startActivity(i); 
        count++; 
        writeSharedPreference(count,"cntSP","cntKey"); 
       } 

       else { 

        Intent i = new Intent(SplashScreen.this, MainActivity.class); 
        startActivity(i); 
        // close this activity 

       } 

现在Oncreat方法

public int readSharedPreferenceInt(String spName,String key){ 
     SharedPreferences sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE); 
     return tempInt = sharedPreferences.getInt(key, 0); 
    } 

    //write shared preferences in integer 
    public void writeSharedPreference(int ammount,String spName,String key){ 

     SharedPreferences sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 

     editor.putInt(key, ammount); 
     editor.commit(); 
    } 
相关问题