SharedPreferences判断程序是否第一次运行
2015-02-05 17:46
423 查看
SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置比如窗口状态,比如我们可以通过SharedPreferences来判断程序是不是第一次运行。
下面的实例将用于介绍怎样通过SharedPreferences来判断程序是否是第一次运行,其实现思路很简单,通过在SharedPreferences中存储键值表示程序是否第一次运行。代码如下:
其中在 onCreate 方法中读取,SharedPreferences 信息,在 onStop 中保存 SharedPreferences信息。注意程序的状态信息一般都在 onStop 保存。
下面的实例将用于介绍怎样通过SharedPreferences来判断程序是否是第一次运行,其实现思路很简单,通过在SharedPreferences中存储键值表示程序是否第一次运行。代码如下:
<span style="color:#33CC00;">public class PreferenceTestMain extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; public static final String FIRST_RUN = "first"; private boolean first; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Restore preferences SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); first = settings.getBoolean(FIRST_RUN, true); if (first) { Toast.makeText(this, "The Application is first run", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "The Application is not first run", Toast.LENGTH_LONG).show(); } } @Override protected void onStop() { super.onStop(); // We need an Editor object to make preference changes. // All objects are from android.context.Context SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); if (first) { editor.putBoolean(FIRST_RUN, false); } // Commit the edits! editor.commit(); } }</span>
其中在 onCreate 方法中读取,SharedPreferences 信息,在 onStop 中保存 SharedPreferences信息。注意程序的状态信息一般都在 onStop 保存。
相关文章推荐
- Android 判断用户是否第一次运行此程序
- Android判断程序是否第一次运行
- iphone开发中用NSUserDefaults 判断程序是否第一次运行
- 如何判断程序是否第一次运行
- ios 判断程序是否是第一次运行
- Android判断程序是否第一次运行
- Android判断程序是否第一次运行
- 使用android SharedPreference判断程序是否第一次运行
- 判断程序是否第一次运行
- 使用sharepreference判断是否是第一次运行程序
- 判断IOS程序是否是第一次运行
- 如何判断程序是否第一次运行
- C#判断程序是否运行
- 在VB和VB.NET程序中判断当前是否运行于开发环境中
- Inno Setup安装、卸载时判断是否程序正在运行
- VC中判断程序是否已运行
- 判断程序是否运行在前台
- Inno 4000 Setup安装、卸载时判断是否程序正在运行,安装完成时自动打开网页的脚本
- 判断是否有全屏程序正在运行(C#)
- C#判断程序是否运行