java - 应用程序警报导致奇怪的错误,直到我按下后退按钮

标签 java android alarm android-alarms alarms

我的应用程序中有一个警报,每 10 分钟通知我一次。它工作正常,但每次当警报通知我时,我的应用程序名称都会在屏幕中间显示为一个框,并且在按下后退按钮之前我无法按任何内容。真的很奇怪!

这是我的代码:

    // Alert Code
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Confirmation");
    alertDialog.setMessage("Are you sure you want to send this report?");
    run(gotDataS);

    // Timer Code
    pendingIntent = PendingIntent.getService(CreateNewForm_3.this, 0,
            new Intent(NewForm_3.this, MyAlarmService.class), 0);
    Send.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            alertDialog.setButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                        int which) {

                        Toast.makeText(NewForm_3.this, "Sent", 0)
                                .show();

                        long firstTime = SystemClock.elapsedRealtime();
                        am = (AlarmManager) getSystemService(ALARM_SERVICE);
                        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                               firstTime + 10 * 1000, 30 * 1000, pendingIntent);
                        // firstTime Will be the current_time + ( 10 * 60 * 1000)  =)
                        // 10* 1000 will be (10 * 60 * 1000)
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                bCancel.performClick();
                            }
                        }, (30 * 1000));
                        // ( 30 * 1000) will be firstTime + ( 2- duration)

                        Intent toRecentCases = new Intent(CreateNewForm_3.this,
                                LMPActivity.class);
                        startActivity(toRecentCases);
                    }
                });

            alertDialog.setButton2("No",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {
                        // Here you can add functions
                    }
                });

            alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialog.show();
        }

    });


    bCancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            am.cancel(pendingIntent);
        }
    });

    //For Notification -3-
    final AlertDialog alertDialog3 = new AlertDialog.Builder(this).create();
    alertDialog3.setTitle("Confirmation");
    alertDialog3.setMessage("Are you sure you want to quite?");

    // Press cancel button it will move user to actvity p
    Cancelb.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            alertDialog3.setButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {

                        Intent toRecentCases = new Intent(NewForm_3.this,
                                                          LMPActivity.class);
                        startActivity(toRecentCases);
                    }
                });

            alertDialog3.setButton2("No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // Doing nothing!
                        }
                    });

            alertDialog3.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialog3.show();
        }
    });

}

这正在显示对话框 Activity

public class ShowingDialog extends Activity {

    boolean b;
    String CancelMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        //For Notification -1-
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Confirmation");
        alertDialog.setMessage("Do you really want it?");

        // For Notification -2-
        final AlertDialog alertDialog2 = new AlertDialog.Builder(this).create();
        alertDialog2.setTitle("Confirmation");
        alertDialog2.setMessage("Are you sure you want it?");

        alertDialog.setButton("yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
               Intent intent= new Intent(ShowingDialog.this,MyPage.class);
               startActivity(intent);
           ;
           }
        });

        alertDialog.setButton2("no",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int which) {

                    alertDialog2.setButton("yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // Here you can add functions
                            // Sending a Message to server that the plaintiff found the case
                            // For Sending SMS with cancel Request
                            // Getting Case_ID + putting it inside CancelMsg
                               CancelMsg = "Case_ID cancel";
                            if (!b) {
                                try {
                                    sendSMS("5556", CancelMsg);
                                    Toast.makeText(ShowingDialog.this, "Sent", Toast.LENGTH_LONG)
                                            .show();
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    Toast.makeText(ShowingDialog.this, e.getMessage(),
                                            Toast.LENGTH_LONG).show();
                                }
                            }
                        }
                    });

                    alertDialog2.setButton2("no", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // Here you can add functions
                            // Do nothing
                        }
                    });

                    alertDialog2.setIcon(android.R.drawable.ic_dialog_alert);
                    alertDialog2.show();
                }
            });

            alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialog.show();
        }

        public void sendSMS(String number, String msg) throws Exception {
            if (!b) {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(number, null, msg, null, null);
            }
            b = true;
        }
    }

list 中:

<activity
    android:name=".ShowingDialog"
    android:theme="@android:style/Theme.Dialog" />

最佳答案

看来您并没有重载后退语句,因此当您点击后退按钮时,您将返回到后退堆栈。 Android 文档对此有更多信息:

Tasks and Back Stack

关于java - 应用程序警报导致奇怪的错误,直到我按下后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10128194/

相关文章:

java - 如何生成 6 个唯一的 java 数字

Android:保存在变量中的 DatePicker 值不会进入 onCreate

java - 如何监控用户是否以正确的流程集成我的 android 库?

java - 如何查看/删除 Google Analytics 中的任何特定用户数据

c - 强制每 1 秒调用一次警报处理程序(在 C 中)

java - 多线程中类加载器的行为

java - 如何让 Selenium firefox 驱动程序仅对浏览过的页面进行屏幕截图

java - 使用 java 进行图像处理

c - 如何在信号驱动的恶魔中暂停主循环?

android - 时间选择器当前时间和时间选择器时间之间的差异 Android 闹钟