如何仅在首次运行应用程序时显示警告对话框?

时间:2011-03-23 18:03:02

标签: java android alertdialog

我有一个警告,我想第一次在第一次启动应用程序后立即显示。

我该怎么做?

1 个答案:

答案 0 :(得分:22)

有几种方法可以做到这一点,但也许最简单的方法就是检查SharedPreferences对象中的标志,并在显示警报后设置它。

SharedPreferences

这样的东西
public class MyActivity extends Activity {

public static final String PREFS_NAME = "MyPrefsFile";

@Override
protected void onCreate(Bundle state){

   super.onCreate(state);
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   boolean dialogShown = settings.getBoolean("dialogShown", false);

   if (!dialogShown) {
     // AlertDialog code here

     SharedPreferences.Editor editor = settings.edit();
     editor.putBoolean("dialogShown", true);
     editor.commit();    
   }
}
相关问题