如何在加载应用程序一段时间后打开Android警报对话框?

时间:2015-12-02 11:15:38

标签: android android-intent android-activity

我想在打开应用程序20秒后显示我的Android警报对话框。我应该在MainActivity.java文件中做出哪些更改?

4 个答案:

答案 0 :(得分:3)

使用此:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 20s
  // Write your code to display AlertDialog here
  }
}, 20000);

答案 1 :(得分:1)

这样做,首先创建Handler以在20秒后打开警告对话框

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            openAlert();
        }
    }, 20000);

这是警报对话代码

private void openAlert() {
    new AlertDialog.Builder(context)
            .setTitle("SignOut")
            .setMessage("TYPE YOUR MESSAGE HERE")
            .setPositiveButton(android.R.string.yes,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // do want you want to do here
                        }
                    })
            .setNegativeButton(android.R.string.no,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                        }
                    }).setIcon(android.R.drawable.ic_dialog_alert).show();

}

希望这会对你有所帮助。

答案 2 :(得分:-1)

创建句柄以在20秒后打开警报

Handler handler = new Handler();
handler.postDelayed(new Runnable() {

    @Override
    public void run() {
        alertDialog();
    }
}, 20000);

创建方法名称alertDialog。

private void alertDialog() {
new AlertDialog.Builder(context)
        .setTitle("SignOut")
        .setMessage("MESSAGE")
        .setPositiveButton(android.R.string.yes,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {
                        // code here
                    }
                })
        .setNegativeButton(android.R.string.no,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {
          // nothing to do
                    }
                }).setIcon(android.R.drawable.ic_dialog_alert).show();}

乐于帮助和快乐编码...

答案 3 :(得分:-2)

在onCreate中使用Handler方法。

#include <stdio.h>
int main (void){
int letters [] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int sum, i, j, count=0;
char numbers [] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};   
char name [50];
char ch;
printf("Enter a name --> ");
for(i=0;i<50;i++){
scanf("%c", name [i]);
++count;
}
for(j=0;j<=count;j++){
    for(i=0;i<26;i++){
        if(name [j] == letters [i])
        sum += numbers[i];
    }
}
printf("Your name is: ");
for(i=0;i<=count;i++)
printf("%c", name[i]);
printf("\n");
printf("The sum of your name is: %d\n", sum);
return (0);
}