繁体   English   中英

显示Toast消息并执行操作经过一段时间后

[英]Display Toast Message & Perform an action When a certain period of time has passed

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

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

这是我的代码到目前为止,将不胜感激任何建议或想法。

使用Handler

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();

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

假设您从initiateDialog方法调用Dialog

    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
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM