在选定的时间和日期设置闹钟

发布于 2024-12-17 17:06:12 字数 1089 浏览 4 评论 0原文

我正在 Android 2.1 上开发任务管理器。我想为按日期选择器中的日期和时间选择器中的时间设置的任务设置闹钟帮助我编写代码。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final EditText next = (EditText) findViewById(R.id.editText1);
    final Button sub = (Button) findViewById(R.id.button1);
    final Button res = (Button) findViewById(R.id.button2);
    final DatePicker dp = (DatePicker) findViewById(R.id.datePicker1);
    final TimePicker tp = (TimePicker) findViewById(R.id.timePicker1);

    sub.setOnClickListener(new View.OnClickListener() {
        public void onClick(final View view) {

             int y=dp.getYear();
             int mo=dp.getMonth();
             int day=dp.getDayOfMonth();

            Time t = new Time();
                                    int h=t.getCurrentHour();
                                    int m=t.getCurrentMinutes();

    }

 private AlarmManager getSystemService(String alarmService) {

            // TODO Auto-generated method stub
            return null;
        }

    });

I am developing a taskmanager on Android 2.1. I want to set alarm for a task set by date from datepicker and time from time picker Help me with the code..

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final EditText next = (EditText) findViewById(R.id.editText1);
    final Button sub = (Button) findViewById(R.id.button1);
    final Button res = (Button) findViewById(R.id.button2);
    final DatePicker dp = (DatePicker) findViewById(R.id.datePicker1);
    final TimePicker tp = (TimePicker) findViewById(R.id.timePicker1);

    sub.setOnClickListener(new View.OnClickListener() {
        public void onClick(final View view) {

             int y=dp.getYear();
             int mo=dp.getMonth();
             int day=dp.getDayOfMonth();

            Time t = new Time();
                                    int h=t.getCurrentHour();
                                    int m=t.getCurrentMinutes();

    }

 private AlarmManager getSystemService(String alarmService) {

            // TODO Auto-generated method stub
            return null;
        }

    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神爱温柔 2024-12-24 17:06:12

嘿,这是如何在 android AlarmManager 上将闹钟设置为特定日期
(android Alarmmanager 在日期设置闹钟)
我一直在寻找这个。
注意这个月的价值!!

Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
//cal.add(Calendar.SECOND, 10);

cal.set(Calendar.DATE,19);  //1-31
cal.set(Calendar.MONTH,Calendar.DECEMBER);  //first month is 0!!! January is zero!!!
cal.set(Calendar.YEAR,2012);//year...

cal.set(Calendar.HOUR_OF_DAY, 16);  //HOUR
cal.set(Calendar.MINUTE, 39);       //MIN
cal.set(Calendar.SECOND, 10);       //SEC


// Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(MainActivity.this, alarmAct.class);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0,intent, 0);

//or if you start an Activity
//PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,intent, 0);

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

Hey this is how to set alarm on android AlarmManager to spesific date
(android alarmmanager set alarm on date)
I have been searching all over for this.
pay attention to the value of the month!!

Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
//cal.add(Calendar.SECOND, 10);

cal.set(Calendar.DATE,19);  //1-31
cal.set(Calendar.MONTH,Calendar.DECEMBER);  //first month is 0!!! January is zero!!!
cal.set(Calendar.YEAR,2012);//year...

cal.set(Calendar.HOUR_OF_DAY, 16);  //HOUR
cal.set(Calendar.MINUTE, 39);       //MIN
cal.set(Calendar.SECOND, 10);       //SEC


// Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(MainActivity.this, alarmAct.class);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0,intent, 0);

//or if you start an Activity
//PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,intent, 0);

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
伴我心暖 2024-12-24 17:06:12

这就是设置闹钟的方法。

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());

//when does this go off?
Long mHowLongFromNowInMilliseconds = 10000 //(10 seconds from now)
calendar.add(Calendar.MILLISECOND, mHowLongFromNowInMilliseconds);

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

日历不是必需的...但它可能会有所帮助;)

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 10000, pendingIntent); 

!!!请记住:当设备完全断电时,警报会取消。

http://developer.android.com/reference/android/app/AlarmManager.html

This is how to set an alarm.

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());

//when does this go off?
Long mHowLongFromNowInMilliseconds = 10000 //(10 seconds from now)
calendar.add(Calendar.MILLISECOND, mHowLongFromNowInMilliseconds);

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

Calendar isnt required... But it can be helpful ;)

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 10000, pendingIntent); 

!!! Remember: Alarms are canceled when the device is powered off completely.

http://developer.android.com/reference/android/app/AlarmManager.html

你穿错了嫁妆 2024-12-24 17:06:12

Android 不会为您播放闹钟,闹钟管理器允许您安排应用程序在将来的某个时间运行。
因此,只需在 AlarmManager 中添加时间和待处理意图,然后在调用该意图时播放音乐。

访问:http://developer.android.com/reference/android/app/AlarmManager .html

Android do not play alarm for you, Alarm Manager allow you to schedule your application to be run at some point in the future.
So, simply add the time and a pending intent in the AlarmManager and when this intent will be invoked, play music.

Visit: http://developer.android.com/reference/android/app/AlarmManager.html

九公里浅绿 2024-12-24 17:06:12
public class AlarmService extends Activity {
    private PendingIntent mAlarmSender;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create an IntentSender that will launch our service, to be scheduled
        // with the alarm manager.
        mAlarmSender = PendingIntent.getService(AlarmService.this,
                0, new Intent(AlarmService.this, AlarmService_Service.class), 0);

        setContentView(R.layout.alarm_service);

        // Watch for button clicks.
        Button button = (Button)findViewById(R.id.start_alarm);
        button.setOnClickListener(mStartAlarmListener);
        button = (Button)findViewById(R.id.stop_alarm);
        button.setOnClickListener(mStopAlarmListener);
    }

    private OnClickListener mStartAlarmListener = new OnClickListener() {
        public void onClick(View v) {
            // We want the alarm to go off 30 seconds from now.
            long firstTime = SystemClock.elapsedRealtime();

            // Schedule the alarm!
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                            firstTime, 30*1000, mAlarmSender);

            // Tell the user about what we did.
            Toast.makeText(AlarmService.this, R.string.repeating_scheduled,
                    Toast.LENGTH_LONG).show();
        }
    };

    private OnClickListener mStopAlarmListener = new OnClickListener() {
        public void onClick(View v) {
            // And cancel the alarm.
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.cancel(mAlarmSender);

            // Tell the user about what we did.
            Toast.makeText(AlarmService.this, R.string.repeating_unscheduled,
                    Toast.LENGTH_LONG).show();

        }
    };
}
public class AlarmService extends Activity {
    private PendingIntent mAlarmSender;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create an IntentSender that will launch our service, to be scheduled
        // with the alarm manager.
        mAlarmSender = PendingIntent.getService(AlarmService.this,
                0, new Intent(AlarmService.this, AlarmService_Service.class), 0);

        setContentView(R.layout.alarm_service);

        // Watch for button clicks.
        Button button = (Button)findViewById(R.id.start_alarm);
        button.setOnClickListener(mStartAlarmListener);
        button = (Button)findViewById(R.id.stop_alarm);
        button.setOnClickListener(mStopAlarmListener);
    }

    private OnClickListener mStartAlarmListener = new OnClickListener() {
        public void onClick(View v) {
            // We want the alarm to go off 30 seconds from now.
            long firstTime = SystemClock.elapsedRealtime();

            // Schedule the alarm!
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                            firstTime, 30*1000, mAlarmSender);

            // Tell the user about what we did.
            Toast.makeText(AlarmService.this, R.string.repeating_scheduled,
                    Toast.LENGTH_LONG).show();
        }
    };

    private OnClickListener mStopAlarmListener = new OnClickListener() {
        public void onClick(View v) {
            // And cancel the alarm.
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.cancel(mAlarmSender);

            // Tell the user about what we did.
            Toast.makeText(AlarmService.this, R.string.repeating_unscheduled,
                    Toast.LENGTH_LONG).show();

        }
    };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文