如何通过首次启动应用程序来显示对话框

时间:2011-12-25 00:10:11

标签: android

第一次启动应用时,是否可以创建仅显示的对话框? 如果有可能,我该如何制作呢?

感谢

2 个答案:

答案 0 :(得分:6)

试试这个,不需要数据库或文件流等。

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main):

    SharedPreferences prefs = getSharedPreferences(filename, 0);
    boolean runOnce = prefs.getBoolean("ranOnce", false);
            if (runOnce == false){
                 //dialog code here
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("ranOnce", true);
            editor.commit();
            }
     //normal onCreate code here
}

它设置一个启动时为false的SharedPreference。如果它是假的,它将运行对话框代码,然后将自己设置为true。一旦它成立,它将不会在下次应用程序启动时运行对话框代码。

答案 1 :(得分:-2)

运行此对话框(或例如toast message)时,您可以将文件保存在缓存目录中

// WRITE

File f_cache = (activity_name).this.getCacheDir();
f_cache_path = f_cache.getAbsolutePath();
OutputStream title_stream = null;
File title_forsave = new File(f_cache_path + File.separator + "info.txt");
title_stream = new FileOutputStream(title_forsave);
title_stream.flush();
title_stream.close();

// READ

FileInputStream title_in = new FileInputStream(f_cache_path + File.separator + "info.txt");

// AND ALL

//read
FileInputStream title_in = new FileInputStream(f_cache_path + File.separator + "info.txt");

if (title_in != null) {
title_in.close();
} else {

YOUR DIALOG FUNCTION (OR OTHER)

// write

File f_cache = (activity_name).this.getCacheDir();
f_cache_path = f_cache.getAbsolutePath();
OutputStream title_stream = null;
File title_forsave = new File(f_cache_path + File.separator + "info.txt");
title_stream = new FileOutputStream(title_forsave);
title_stream.flush();
title_stream.close();

}
  • 用户可以在需要时清除缓存并再次查看您的功能。