显示Toast Message&执行操作经过一段时间后

时间:2016-05-06 14:32:50

标签: java android

我有一个应用程序,它通过一个数字文件读取。预定义阈值,并且将文件中的每个数字逐个地与阈值进行比较。当读取的数字大于阈值时,使用按钮显示警报。如果按住按钮2分钟,请发送短信。如果按钮仍然没有按下另外两分钟,则拨打电话。

我正在努力的一点是计算过去的时间并在经过一段时间后触发前面提到的两种方法之一。如何定义它和在哪里。我看过很多使用Handler,timers和Runnable方法的例子,尝试过没有成功。当我运行应用程序时,没有任何反应。

到目前为止,这是我的代码,会感谢任何建议或想法。

使用处理程序

private static final int MISS_LIMIT = 1000;
int misses = 0;

    final Handler handler = new Handler();
    final Runnable timer = new Runnable() {
        @Override
        public void run()
        {
            handler.postDelayed(timer, MISS_LIMIT);
            // user too late: increment miss counter
            if (++misses >= MISS_LIMIT)
            {
                //TODO miss limit reached
                Toast.makeText(MainActivity.this, "Time is passed", Toast.LENGTH_SHORT).show();
                finish(); // close this activity
            }
        }
    };

其余的代码:

 int Threshold = 40;

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

    File dir = Environment.getExternalStorageDirectory();


    File file = new File(dir, "/Numbers.txt");

    try
    {
       FileInputStream fin = new FileInputStream(file);

       if (fin != null)
       {

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fin));
        String line ="";

        while((line= bufferedReader.readLine()) != null)
        {

          if (Integer.parseInt(line) > Threshold)
          {
             //store value in integer
             int number = Integer.parseInt(line);

             // create alert dialog
             AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                                    builder
           .setTitle("Alert !!")
           .setMessage("High Temperature !!" + "\t" + number)


           .setPositiveButton("Send SMS",new DialogInterface.OnClickListener()
            {
            public void onClick(DialogInterface dialog,int id)
              {
                // if this button is clicked, close current activity
                MainActivity.this.finish();
              }
            });


    AlertDialog alertDialog = builder.create();
                                          alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.holo_red_light);
        alertDialog.show();
         }

    }

    fin.close();

1 个答案:

答案 0 :(得分:0)

可能您可以进行以下更改以便工作。

假设您正在调用initiateDialog方法的对话框

    void initiateDialog(){
    //Show Alert
    //Your existing code for Dialog
    AlertDialog alertDialog = builder.create();
    alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.holo_red_light);
    alertDialog.show();
    new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    buttonNotPressedFirst();
                }
            }, FIRST_DELAY);

    }
    void buttonNotPressedFirst(){
         //SEND SMS
    new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    buttonNotPressedSecond();
                }
            }, SECOND_DELAY);
    }
    }

    void buttonNotPressedSecond(){
      //Proceed with phone call
    }