2

如何在特定时间和日期启动警报,我在哪里使用TimePickerand 获得时间和日期DatePicker。即使我可以在用户指定的时间启动警报,但我不能在用户指定的日期这样做。我怎样才能达到我的目标?我已经完成的代码。

package com.example.modifiedalarm;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;

public class MainActivity extends Activity {
    Button b1, b2, b3;
    Calendar c;
    String abcd;
    TextView ed, tv;
    int hr, min, year, month, date;
    int time = 0;
    int daa = 0;

private PendingIntent pendingIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        ed = (TextView) findViewById(R.id.settime);
        tv = (TextView) findViewById(R.id.gettime);
        c = Calendar.getInstance();

        hr = c.get(Calendar.HOUR_OF_DAY);
        min = c.get(Calendar.MINUTE);
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        date = c.get(Calendar.DAY_OF_MONTH);

        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
            i.putExtra(AlarmClock.EXTRA_MESSAGE, "New Alarm");
            i.putExtra(AlarmClock.EXTRA_HOUR, hr);
            i.putExtra(AlarmClock.EXTRA_MINUTES, min);
            startActivity(i);
}});                

        b1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // showDialog(time);
                new TimePickerDialog(MainActivity.this, timeval, hr, min, true)
                        .show();

            }

        });

        b3.setOnClickListener(new OnClickListener() {

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

                new DatePickerDialog(MainActivity.this, dateval, year, month,
                        date).show();
            }
        });
    }


    OnDateSetListener dateval = new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int Year, int monthOfYear,
                int dayOfMonth) {
            // TODO Auto-generated method stub
            year = Year;
            month = monthOfYear;
            date = dayOfMonth;
            updatedate();
        }
    };


    OnTimeSetListener timeval = new OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub
            hr = hourOfDay;
            min = minute;
            updatetime();
        }
    };

    public void updatedate() {
        tv.setText(new StringBuilder().append(date).append("/").append(month).append("/").append(year));
        String og=tv.getText().toString();
        //int ogx=Integer.parseInt(og);
        Toast.makeText(MainActivity.this, "the date is "+og, Toast.LENGTH_LONG).show();
        datecompare();

}   private void datecompare() {

    }
private void updatetime() {
        // TODO Auto-generated method stub
        ed.setText(new StringBuilder().append(hr).append(":").append(min));
        String b=hr + ":" + min;
        }

}

例如:我想在 2013 年 5 月 4 日下午 12:00 设置一个闹钟。我怎样才能做到这一点?

4

2 回答 2

1

这是我从现在开始设置 5 分钟的注销警报的方法

Calendar cal1 = Calendar.getInstance(); 
cal1.add(Calendar.MINUTE, 5); 

您可以使用Calendar中提供的方法和字段对日期进行更多操作。例如:

Calendar cal = Calendar.getInstanc();
cal.set(Calendar.MONTH, JANUARY);

设置月份。检查该链接,您可以看到可以设置它的所有不同方式。用于set()设置字段(月、日等)并用于add()添加到字段。无论如何,这应该可以帮助您入门

编辑

无论您在哪里设置待处理的意图,只需使用您设置的这些变量作为值

    private void updatedate() {
    // TODO Auto-generated method stub
    c.set(Calendar.Month, month);            // `c` is the `Calendar` instance you defined earlier. Now we are setting the month on that instance
    c.set(Calendar.DAY_OF_MONTH, date);
    }

然后当你点击你的Button

  b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
         long alarmTime = cal.getTimeInMillis(); // convert your calendar instance with the dates set to millis
            Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
        i.putExtra(AlarmClock.EXTRA_MESSAGE, "New Alarm");
        i.putExtra("alarmTime", alarmTime);     //"alarmTime" will be used to get the time to set the alarm in your AlarmClock class
        startActivity(i);
}}); 

如果你已经设定了时间,剩下的你就知道该怎么做了。希望这可以帮助你

于 2013-04-03T12:52:27.073 回答
1
    AlarmManager:
    Calendar cal = Calendar.getInstance();

            long when =0; 
            intent = new Intent(this, AlarmReceiver.class);
            intent.putExtra("eventName", eventName);
            intent.putExtra("eventDescription",eventDescription);

            // Get the AlarmManager service
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

            RadioGroup rg=(RadioGroup)findViewById(R.id.Setting_rgRepeatMode);
            selectedRadio=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
            long repeatTime=0;

                cal.set(mYear,mMonth,mDay,mHour,mMinute,0);
                intent.putExtra("Flag",true);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                when=cal.getTimeInMillis();
                am.set(AlarmManager.RTC_WAKEUP,when, pendingIntent);
                return;

    BroadCastReceiver:

        public class MyBroadcastReceiver extends BroadcastReceiver {
       try {
//Here you can write your logic
                    Toast.makeText(context,bundle.getString("eventName"), Toast.LENGTH_SHORT).show();

            NotificationManager notificationManager =(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
            int icon = R.drawable.event;
            CharSequence notiText = "Event Notification";
            long time = System.currentTimeMillis();
            @SuppressWarnings("deprecation")
            Notification notification = new Notification(icon, notiText,time);
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            Intent notificationIntent = new Intent(context, Setting.class);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context,intent.getStringExtra("eventName"),intent.getStringExtra("eventDescription"), contentIntent);
            int SERVER_DATA_RECEIVED =  1;
            Calendar cal=Calendar.getInstance();
            Toast.makeText(context,"aavechhe "+intent.getBooleanExtra("Flag",false),Toast.LENGTH_SHORT).show();
            if(intent.getIntExtra("month",0)==cal.get(Calendar.DAY_OF_MONTH))
            {
                Toast.makeText(context,"aavechhe "+cal.get(Calendar.DAY_OF_MONTH),Toast.LENGTH_SHORT).show();
                notificationManager.notify(SERVER_DATA_RECEIVED, notification);
            }
            if(intent.getBooleanExtra("Flag",false))
                notificationManager.notify(SERVER_DATA_RECEIVED, notification);

        } catch (Exception e) {
            Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
            e.printStackTrace();

        }
于 2013-04-03T13:01:38.120 回答