如何在android中每周安排闹钟?

Posted

技术标签:

【中文标题】如何在android中每周安排闹钟?【英文标题】:How to schedule alarm weekly in android ? 【发布时间】:2014-04-08 21:48:31 【问题描述】:

我想根据所需的日期安排闹钟,例如周日,周二,周四。我已经按时间安排了,但不能像工作日那样做。

下面是代码:

private void StartScheduler(Context context) 

       Calendar startTimmer = Calendar.getInstance();
       startTimmer.setTimeZone(TimeZone.getDefault());        
       startTimmer.set(Calendar.HOUR_OF_DAY,sHour);
       startTimmer.set(Calendar.MINUTE,sMinute);
       startTimmer.set(Calendar.SECOND,00); 

       AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);         
       Intent StartAlarmTime = new Intent(Schedule_details.this, MyAlaramStartReceiver.class);         
       PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, StartAlarmTime, 0);

       alarmManager.set(AlarmManager.RTC_WAKEUP, startTimmer.getTimeInMillis(),pendingIntent1);     
       Log.d("<<My-Start-Alarm>>", "ALARM Set To Start At: " + startTimmer.getTime().toLocaleString());

【问题讨论】:

【参考方案1】:

答案如下:

public class MyReceiver extends BroadcastReceiver 
DBAdapter mDba;
SQLiteDatabase mDb;
Ringtone rt;
MediaPlayer mp;
AlertDialog.Builder alertbox;
Context ctx;

@Override
public void onReceive(Context context, Intent intent) 

    DBHelper mDbh = new DBHelper(context, null, null, 1);
    mDb = mDbh.getWritableDatabase();
    mDb.setLockingEnabled(true);
    mDba = new DBAdapter(context);
    mDba.open();
    Cursor cr = mDb.query("mReminderEntry", null, null, null, null,
            null, null);
    if (cr.equals(null)) 
        System.out.println("No Data Found");
     else 
        Date d = new Date();
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        String today = null;
        if (day == 2) 
            today = "Monday";
         else if (day == 3) 
            today = "Tuesday";
         else if (day == 4) 
            today = "Wednesday";
         else if (day == 5) 
            today = "Thursday";
         else if (day == 6) 
            today = "Friday";
         else if (day == 7) 
            today = "Saturday";
         else if (day == 1) 
            today = "Sunday";
        

        int system_hour = d.getHours();
        int system_minute = d.getMinutes();
        cr.moveToFirst();
        for (int i = 0; i < cr.getCount(); i++) 
            if (cr.getString(3).equals(system_hour + ":" + system_minute)
                    && cr.getString(1).equals("Daily")) 
                Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
                scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(scheduledIntent);

                break;

             else if (cr.getString(3).equals(
                    system_hour + ":" + system_minute)
                    && cr.getString(1).equals(today)) 

                Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
                scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(scheduledIntent);

                break;
             else 
                System.out.println("No Matching");
            
            cr.moveToNext();
        
    
    cr.close();
    mDba.close();

【讨论】:

【参考方案2】:

试试这样的

Calendar calendar = Calendar.getInstance();

     int day = calendar.get(Calendar.DAY_OF_WEEK);

    alarmManager.set(AlarmManager.RTC_WAKEUP, startTimmer.getTimeInMillis(),day * 7, pendingIntent1);  

【讨论】:

不,我必须在数据库中设置值,而不是从那里获取值并设置任何其他想法......

以上是关于如何在android中每周安排闹钟?的主要内容,如果未能解决你的问题,请参考以下文章

使用 AlarmManager/BroadcastReceiver 在 Android 中每周一重复闹钟

Android - 如何从我自己的应用程序设置系统闹钟

每2分钟安排一次闹钟android

如何在 Heroku 上安排“每周”工作?

在 Android 中的每周一安排通知

如何在 apache cordova 上安排活动?