草庐IT

android - 如何在android中为工作日设置闹钟

coder 2023-11-30 原文

我有一个场景

设置闹钟(周一到周五)

假设我选择的时间是:hour = 9, minutes = 15, am_pm = "AM"

现在我想为每个星期一到星期五上午 9:15 设置闹钟

下面的代码我试过了,但没有得到想要的结果。

if(choice.equals("Week Days (Mon-Fri)"))
{
    for(int a = 2; a <= 5; a++) //here I am assuming a is from 2 to 5 (calendar DAY_OF_WEEK from Monday to Friday)
    {
        Calendar alarmCalendar = Calendar.getInstance();

        alarmCalendar.set(Calendar.HOUR_OF_DAY, _hourOfDay);

        alarmCalendar.set(Calendar.MINUTE, _minute);

        alarmCalendar.set(Calendar.SECOND, 0);

        alarmCalendar.set(Calendar.MILLISECOND, 0);

        if(am_pm.equals("AM"))
        {
            alarmCalendar.set(Calendar.AM_PM, 0);
        }
        else
        {
            alarmCalendar.set(Calendar.AM_PM, 1);
        }


        alarmCalendar.set(Calendar.DAY_OF_WEEK, a);

        Long alarmTime = alarmCalendar.getTimeInMillis();

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
                alarmTime, 24 * 60 * 60 * 1000 , pendingIntent);
    }

    Toast.makeText(ActivityReminder.this, "Meeting Reminder Set on Week Days (Mon-Fri)", 
                        Toast.LENGTH_LONG).show();
}

我使用过 BroadcastReceiver,例如:

public class AlarmReceiver extends BroadcastReceiver
{
    NotificationManager mNotificationManager;

    Context context;

    public static final String TAG = "Reminder...";

    @Override
    public void onReceive(Context context, Intent intent)
    {
        this.context = context;

        String subject = "<h3>Meeting Reminder: </h3>" + intent.getStringExtra("subject");

        Toast.makeText(context, Html.fromHtml(subject), Toast.LENGTH_LONG).show();

        showNotification(subject);
    }

    @SuppressWarnings("deprecation")
    private void showNotification(String msg) 
    {
        Intent notificationIntent = new Intent(context.getApplicationContext(),
                ActivityMainScreen.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

        stackBuilder.addParentStack(ActivityMainGeofence.class);

        stackBuilder.addNextIntent(notificationIntent);

        String arrivalTime = TimeUtil.toString(Calendar.getInstance().getTime(), 
                "dd-MM-yyyy hh:mm:ss a");

        notificationIntent.putExtra("subject", msg)
        .putExtra("time", arrivalTime).putExtra("type", "Meetings/Reminder");

        PendingIntent notificationPendingIntent = stackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new Notification();
        notification.icon = R.drawable.app_icon;

        notification.setLatestEventInfo(context.getApplicationContext(), 
                "WFM Meeting Reminder", Html.fromHtml(msg), notificationPendingIntent);

        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notification.defaults |= Notification.DEFAULT_SOUND;

        notification.defaults |= Notification.DEFAULT_VIBRATE;

        NotificationManager mNotificationManager = (NotificationManager) 
                context.getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(0, notification);

    }
}

最后在 list 中:

<receiver android:name="com.my_package_name.AlarmReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
</receiver>

已编辑: 在这里,我想说明 One Time 闹钟对我有用,但对于上述选择,Week Days (Mon-Fri) 无法正常工作

工作代码是:

if(choice.equals("One Time"))
{
    alarmManager.set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent); // here PERIOD is the time selected by me in milliseconds...

    Toast.makeText(ActivityReminder.this, "Meeting Reminder Set One Time", 
            Toast.LENGTH_LONG).show();
}

我哪里做错了。任何帮助将不胜感激。

最佳答案

我会假设闹钟只设置在星期五的 9:15?这应该是因为 AlarmManager 文档中的以下行:

如果已经为同一个 IntentSender 安排了一个警报,它将首先被取消。 http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int , 长, 长, android.app.PendingIntent)

为了做你想做的事,你要么需要 5 个 PendingIntents,要么只为第一个事件设置一个警报,当收到该警报时,你可以为第二天设置警报,依此类推。

我可能会选择第二个选项,因为在第一个方法中您需要 5 个不同的 PendingIntents,这意味着它们的 requestCode 或支持 Intent 必须不同(具有不同的类型、操作或类别)。

关于android - 如何在android中为工作日设置闹钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26859959/

有关android - 如何在android中为工作日设置闹钟的更多相关文章

  1. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  2. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  5. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  6. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  7. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  8. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  9. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  10. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

随机推荐