簡體   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