如果在10秒内未再次按下按钮,请发送提醒

时间:2017-04-01 00:21:52

标签: android countdowntimer

我有一个按钮("检查"),它开始倒计时,当达到零时,手机会振动。按"检查"再次关闭振动,过程重新开始。按"警告"随时向预先指定的号码发送短信。到目前为止,这是我的代码,就此而言,它工作得很好。但我想连接这些功能,以便一旦振动开始,用户有十秒钟的时间点击"检查"并重新启动计时器,否则"警告"功能将启动,发送短信。我该怎么做?我需要制作另一个倒数计时器吗?

"检查"按钮代码到目前为止:

check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vibe.cancel();
                if (countDownTimer != null)
                    countDownTimer.cancel();

                countDownTimer = new CountDownTimer(timerTime * 1000, 1000) {

                    @Override
                    public void onTick(long milliseconds) {
                        long minutes = milliseconds / 60000;
                        long seconds = (milliseconds / 1000) % 60;
                        minsLeft.setText(getString(R.string.minutes) + (int) (minutes) + "  seconds:" + (int) (seconds));

                    }

                    @Override
                    public void onFinish() {
                        minsLeft.setText("Are You OK?");
                        long[] pattern = {100, 1000, 100, 2};
                        vibe.vibrate(pattern, 1);
                    }
                }.start();
            }
        });
    }

"警报"按钮代码(如果"检查"再次按下它将被调用):

    public void onAlertClick(View view) {
        Toast sent = Toast.makeText(getApplicationContext(), "Alert Sent", Toast.LENGTH_SHORT);
        Toast permissions = Toast.makeText(getApplicationContext(), "Please change app permissions in settings", Toast.LENGTH_SHORT);


        try {
            SmsManager.getDefault().sendTextMessage(getString(R.string.smsNumber), null,
                    getString(R.string.smsMessage), null, null);
            sent.show();

        } catch (Exception e) {
            permissions.show();
        }
    }

1 个答案:

答案 0 :(得分:0)

我要做的是,在第一个计时器完成时创建一个对话框片段,然后在该对话框片段中有另一个计时器倒计时10秒 - 如果对话框计时器达到0,则执行警报方法:

(p.s。你可以将这个课程嵌入你的主课程中,这样你就可以更容易地调用你的第一个倒数计时器了)

public static class SessionExpiringDialog extends DialogFragment {
    CountDownTimer countDownTimer2;
    public SessionExpiringDialog() {
        super();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View view = inflater.inflate(R.layout.session_dialog, null);
        final TextView sessionText = (TextView) view.findViewById(R.id.session_text);
        countDownTimer2 = new CountDownTimer2(11000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                long diffSeconds = millisUntilFinished / 1000 % 60;
                sessionText.setText("SMS Alert will be sent in: " + diffSeconds + " seconds.");
            }

            @Override
            public void onFinish() {
                onAlertClick(view);
            }
        };
        countDownTimer2.start();
        builder.setView(view);
        builder.setMessage("Alert SMS Pending").setTitle("ATTENTION:")
                .setPositiveButton("I'm OKAY!", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        countDownTimer2.cancel();
                        countDownTimer.start();
                        dismiss();
                    }
                });
        return builder.create();
    }
}

此代码假定存在一个名为res / layout / session_dialog.xml的布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/session_text"
    android:textSize="20sp"
    android:textStyle="bold"
    android:layout_gravity="center"

    />

</LinearLayout>

然后调用此代码,将其放在第一个倒数计时器的onFinish()方法中:

check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            vibe.cancel();
            if (countDownTimer != null)
                countDownTimer.cancel();

            countDownTimer = new CountDownTimer(timerTime * 1000, 1000) {

                @Override
                public void onTick(long milliseconds) {
                    long minutes = milliseconds / 60000;
                    long seconds = (milliseconds / 1000) % 60;
                    minsLeft.setText(getString(R.string.minutes) + (int) (minutes) + "  seconds:" + (int) (seconds));

                }

                @Override
                public void onFinish() {
                    minsLeft.setText("Are You OK?");
                    long[] pattern = {100, 1000, 100, 2};
                    vibe.vibrate(pattern, 1);
                    SessionExpiringDialog sessionExpiringDialog = new SessionExpiringDialog();
                    sessionExpiringDialog.setCancelable(false);
                    sessionExpiringDialog.show(getSupportFragmentManager(), "tag");
                }
            }.start();
        }
    });
}

最后,我还没有看到你在onAlertClick()方法中传递视图的原因,但我继续将其包含在我的粘贴代码中