每个应用启动时如何显示提醒对话框

时间:2017-05-12 16:14:14

标签: android alertdialog

我正在创建一个应用程序,在启动应用程序的主要活动时显示警告对话框,但如果我从另一个活动返回主活动,则会再次显示警报对话框。我希望在应用启动时显示一次警报对话框,该怎么做?

 if(isFirstRun) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

        alertDialog.setTitle("Alert");
        alertDialog.setMessage("You need to have Mobile data or Wifi to access all features.");
        alertDialog.setIcon(R.drawable.ic_error);

        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        alertDialog.show();
    }

    isFirstRun = false;

工作代码: -

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();

    boolean isFirstRun = sharedPreferences.getBoolean("IS_FIRST_RUN", true);

    if(isFirstRun == true) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

        alertDialog.setTitle("Alert");
        alertDialog.setMessage("You need to have Mobile data or Wifi to access all features.");
        alertDialog.setIcon(R.drawable.ic_error);

        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        alertDialog.show();

        // Update
        editor.putBoolean("IS_FIRST_RUN", false);
        editor.commit();
    }

覆盖onDestroy方法: -

@Override
protected void onDestroy() {
    super.onDestroy();

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();

    // Update
    editor.putBoolean("IS_FIRST_RUN", true);
    editor.commit();
}

5 个答案:

答案 0 :(得分:0)

使用isFirstRun永久存储MainActivity的值以供日后使用。

onCreate()SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = sharedPreferences.edit(); boolean isFirstRun = sharedPreferences.getBoolean("IS_FIRST_RUN", false); if(isFirstRun) { AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); alertDialog.setTitle("Alert"); alertDialog.setMessage("You need to have Mobile data or Wifi to access all features."); alertDialog.setIcon(R.drawable.ic_error); alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); alertDialog.show(); // Update editor.putBoolean("IS_FIRST_RUN", true); editor.commit(); } 方法中添加以下代码行:

IS_FIRST_RUN

onDestroy()重置为您的活动@Override protected void onDestroy() { super.onDestroy(); PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = sharedPreferences.edit(); // Update editor.putBoolean("IS_FIRST_RUN", false); editor.commit(); } 方法:

class

OR,您可以创建从Application类扩展的onDestroy(),并在其naturalWidth方法中添加上述重置操作。

希望这会有所帮助〜

答案 1 :(得分:-1)

从应用程序类中取消它。试试这样的事情

MyApplication extends Application {
    override public void onCreate() {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                 // Show your dialog
            }  
        });
    }
}

只是一个单挑,我刚刚起草了这个,可能需要一些修改,但这是一般的要点。

答案 2 :(得分:-1)

尝试将isFirstRun设为静态。这使变量成为类变量,而不是实例变量。

在此定义: https://softwareengineering.stackexchange.com/questions/293478/what-are-the-differences-between-class-variables-and-instance-variables-in-java

因此,一旦将此变量更改为false,它将保持为false,直到应用程序完全关闭然后再次打开。也就是说,当你从另一个应用程序回来时它仍然是错误的。

MyApplication extends Application { 

private static isFirstRun = true;

@override 
public void onCreate() {
    if(isFirstRun) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

        alertDialog.setTitle("Alert");
        alertDialog.setMessage("You need to have Mobile data or Wifi to access all features.");
        alertDialog.setIcon(R.drawable.ic_error);

        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        alertDialog.show();
    }

    isFirstRun = false;
}

}

答案 3 :(得分:-1)

在该问题上使用sharedpreference,它会更简单,

以下是你如何击败它:

首先在onCreate方法中首先在您的应用中初始化sharedPreference:

    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
    Editor editor = pref.edit();
    editor.putBoolean(IS_FIRST_RUN, true);
    editor.commit();

并且不要忘记声明此字符串:

private static final String IS_FIRST_RUN = "IsFirstRun";

因此,自isFirstRun我们的方法以来,我们必须加入一些共享偏好的逻辑,然后检查isFirstRun == True是否显示对话框,或者做其他事情。

// Get isFirstRun State here 
    public boolean isFirstRun(){
        return pref.getBoolean(IS_FIRST_RUN, true);
    }

然后你带上你的代码说:

     if(isFirstRun) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

                alertDialog.setTitle("Alert");
                alertDialog.setMessage("You need to have Mobile data or Wifi to access all features.");
                alertDialog.setIcon(R.drawable.ic_error);

                alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

                alertDialog.show();

     SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
        Editor editor = pref.edit();
        editor.putBoolean(IS_FIRST_RUN, false);
       editor.commit();
            }

    else {
    // do something 
    }

答案 4 :(得分:-1)

我这样做的方法是实现一个唯一的Activity,它只显示AlertDialog并具有透明主题。确认或取消AlertDialog后,您可以将您现有的活动作为意图启动。或者也许添加一个解雇听众。

底线是如果对话框没有功能,因为MainActivity的例程部分不应该构成其设计的一部分。分开责任。

@Override 
public void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
 setContentView(R.layout.transparent);

AlertDialog.Builder alertDialog = new AlertDialog.Builder(DialogActivity.this);

alertDialog.setTitle("Alert");
alertDialog.setMessage("You need to have Mobile data or Wifi to access all features.");
alertDialog.setIcon(R.drawable.ic_error);

alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Intent i = new Intent( DialogActivity.this, MainActivity.class );
        startActivity(i);
        DialogActivity.this.finish();
    }

}); }

确保将所需的活动(DialogActivity)设置为启动器。

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>