CountDown计时器直到Android的特定日期

时间:2016-01-12 17:31:56

标签: android timer scheduled-tasks countdown countdowntimer

我正在尝试创建一个从用户那里获取日期和时间的应用,当该特定日期和时间到来时,用户应该收到通知,应用应该自行启动。 首先我的代码只工作一天(意味着一天的压缩日) 我不知道如何在几天内进行计算,就像用户从现在开始选择3天一样。 所以请帮忙 这是我的所有代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="34dp"
    android:layout_marginTop="58dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/et_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:focusable="false"
    android:hint="select date"
    android:padding="10dp"
    android:singleLine="true"
    android:textColor="#000000"
    android:textSize="15sp" />

<EditText
    android:id="@+id/et_time"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:focusable="false"
    android:hint="select time"
    android:padding="10dp"
    android:singleLine="true"
    android:textColor="#000000"
    android:textSize="15sp" />

<Button
    android:id="@+id/btn_setTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="10dp"
    android:text="submit"
    android:textColor="#ffffff" />

我的主要活动

公共类MainActivity扩展了Activity {

TextView text1;
EditText et_date, et_time;
int mYear, mMonth, mDay, mMinute, mHour; // selected date time
Button btn_setTime;
int Hour, Minute, hourDiff, minDiff, cday, cmonth, cyear; // current time
String date;

private static final String FORMAT = "%02d:%02d:%02d";

int seconds, minutes;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text1 = (TextView) findViewById(R.id.textView1);
    et_date = (EditText) findViewById(R.id.et_date);
    et_time = (EditText) findViewById(R.id.et_time);
    btn_setTime = (Button) findViewById(R.id.btn_setTime);

    et_date.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            // To show current date in the datepicker
            Calendar mcurrentDate = Calendar.getInstance();
            mYear = mcurrentDate.get(Calendar.YEAR);
            mMonth = mcurrentDate.get(Calendar.MONTH);
            mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog mDatePicker = new DatePickerDialog(
                    MainActivity.this, new OnDateSetListener() {
                        public void onDateSet(DatePicker datepicker,
                                int selectedyear, int selectedmonth,
                                int selectedday) {
                            /* Your code to get date and time */
                            mYear = selectedyear;
                            mMonth = selectedmonth;
                            mDay = selectedday;

                            et_date.setText(new StringBuilder()
                                    // Month is 0 based so add 1
                                    .append(mDay).append("-")
                                    .append(mMonth + 1).append("-")
                                    .append(mYear));
                        }
                    }, mYear, mMonth, mDay);
            mDatePicker.getDatePicker().setMinDate(
                    System.currentTimeMillis() - 1000);
            mDatePicker.setTitle("Select date");
            mDatePicker.show();

        }
    });

    et_time.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Calendar mcurrentTime = Calendar.getInstance();
            int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            int minute = mcurrentTime.get(Calendar.MINUTE);

            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(MainActivity.this,
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker,
                                int selectedHour, int selectedMinute) {
                            // String format = "";
                            // if (selectedHour == 0) {
                            // selectedHour += 12;
                            // format = "AM";
                            // } else if (selectedHour == 12) {
                            // format = "PM";
                            // } else if (selectedHour > 12) {
                            // selectedHour -= 12;
                            // format = "PM";
                            // } else {
                            // format = "AM";
                            // }
                            mHour = selectedHour;
                            mMinute = selectedMinute;
                            et_time.setText(new StringBuilder()
                                    .append(selectedHour).append(" : ")
                                    .append(selectedMinute));

                        }
                    }, hour, minute, true);// Yes 24 hour time
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();

        }
    });

    btn_setTime.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            // if(et_date)

            Calendar cal = Calendar.getInstance();
            Hour = cal.get(Calendar.HOUR_OF_DAY);
            Minute = cal.get(Calendar.MINUTE);

            cday = cal.get(Calendar.DATE);
            cmonth = cal.get(Calendar.MONTH);
            cyear = cal.get(Calendar.YEAR);

            int dday, dmonth, dyear;
            dday = mDay - cday;
            dmonth = mMonth - cmonth;
            dyear = mYear - cyear;

            if (dyear == 0) {
                if (dmonth == 0) {
                    if (dday == 0) {
                        // for current day.. calculate only time!

                        long CurrentMilli = (mHour * 3600 * 1000)
                                + (mMinute * 60 * 1000);
                        long SelectedMilli = (Hour * 3600 * 1000)
                                + (Minute * 60 * 1000);

                        long diff = CurrentMilli - SelectedMilli;
                        new CountDownTimer(diff, 1000) { // adjust the milli
                            // seconds
                            // here

                            public void onTick(long millisUntilFinished) {

                                text1.setText(""
                                        + String.format(
                                                FORMAT,
                                                TimeUnit.MILLISECONDS
                                                        .toHours(millisUntilFinished),
                                                TimeUnit.MILLISECONDS
                                                        .toMinutes(millisUntilFinished)
                                                        - TimeUnit.HOURS
                                                                .toMinutes(TimeUnit.MILLISECONDS
                                                                        .toHours(millisUntilFinished)),
                                                TimeUnit.MILLISECONDS
                                                        .toSeconds(millisUntilFinished)
                                                        - TimeUnit.MINUTES
                                                                .toSeconds(TimeUnit.MILLISECONDS
                                                                        .toMinutes(millisUntilFinished))));
                            }

                            public void onFinish() {
                                text1.setText("done!");
                            }
                        }.start();

                    } else {
                        Toast.makeText(getApplicationContext(),
                                "inside current day's else",
                                Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(),
                            "inside current month's else",
                            Toast.LENGTH_SHORT).show();
                }

            } else {
                Toast.makeText(getApplicationContext(),
                        "inside current year's else", Toast.LENGTH_SHORT)
                        .show();
            }

        }
    });

}

}

0 个答案:

没有答案