【问题标题】:Why AlertDialog show when I do not want为什么在我不想要的时候显示 AlertDialog
【发布时间】:2014-09-22 15:28:05
【问题描述】:

我在 AlarmManager 中显示 AlertDialog。 DialogNotification(警报对话框) - 清单中的活动:

<activity
        android:name=".notification.DialogNotification"
        android:label="@string/title_activity_dialog_notification"
        android:theme="@android:style/Theme.Translucent.NoTitleBar">
    </activity>

我希望即使程序没有运行也能显示对话,但是当我进入应用程序时,对话框会在启动画面中显示,但我没有给他打电话。我该如何解决?

public class DialogNotification extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d("Log", "onCreate DialogNotification");

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.app_icon)
                .setTitle(R.string.time_to_call_your_clients)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent notificationIntent = new Intent(DialogNotification.this, SplashActivity.class);
                        startActivity(notificationIntent);
                        dialog.cancel();
                        DialogNotification.this.finish();
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        DialogNotification.this.finish();
                    }
                }).show();
    }
}

【问题讨论】:

  • 那是因为您正在调用 onCreate 上的对话框。每次打开应用都会显示
  • 在 .show() 调用上放一个断点......当你看到堆栈和你所在的函数时应该很明显......

标签: android android-activity dialog


【解决方案1】:

我对您的陈述“我希望即使程序没有运行也能显示对话”有点困惑,因为如果程序没有运行,我不太确定对话应该如何显示。另外,如果不了解您的程序流程,我无法准确指出问题出在哪里。

话虽如此,这是我的猜测。如果您在进入 DialogNotification Activity 时看到此事件,那么问题可能是您每次调用“onCreate”时都在创建一个新的AlertDialog.Builder

如果您想要一个 AlertDialog 实例,那么您可能想要添加一个条件语句(if 语句)以确保您想要创建一个新的 AlertDialog

【讨论】:

    【解决方案2】:

    我发现了我的问题!我启动了启动DialogActivity的服务,方法onStartCommand是:

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //some code 
        return super.onStartCommand(intent, flags, startId);
    }
    

    这不是正确的解决方案!

    是正确的解决方案:

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //some code 
        stopSelf();
        return START_NOT_STICKY;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多