在 Android 应用程序中创建提醒

发布于 2024-12-08 18:46:37 字数 1383 浏览 0 评论 0原文

我正在尝试在我的应用程序中创建每周提醒。为此,我使用 AlarmManager。 这是生成警报的代码

pendingIntent = PendingIntent.getBroadcast(SettingsActivity.this, 1234567, new Intent(SettingsActivity.this, WeeklyReminder.class), 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);                  
Calendar calendar = Calendar.getInstance();       
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
long updateFreq = 30*1000;//24*60*60*1000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), updateFreq, pendingIntent);

这是扩展广播的每周剩余类

public class WeeklyReminder extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        // TODO Auto-generated method stub
        try
        {
            System.out.println("Alarm Initiated");
            Toast.makeText(context, "Recieved Alarm", Toast.LENGTH_SHORT).show();
        } 
        catch (Exception e) 
        {
            Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

}

在 androidmanifest.xml 中,我在标签之前有此条目,

<receiver  android:name="WeeklyReminder">

我希望即使在应用程序关闭时也能调用提醒。但现在什么也没发生 这是正确的做法吗? 谢谢

I am trying to create a weekly reminder in my app. For this I am using AlarmManager.
Here is the code generating alarm

pendingIntent = PendingIntent.getBroadcast(SettingsActivity.this, 1234567, new Intent(SettingsActivity.this, WeeklyReminder.class), 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);                  
Calendar calendar = Calendar.getInstance();       
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
long updateFreq = 30*1000;//24*60*60*1000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), updateFreq, pendingIntent);

This is the weekly remainder class which extends broadcast

public class WeeklyReminder extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        // TODO Auto-generated method stub
        try
        {
            System.out.println("Alarm Initiated");
            Toast.makeText(context, "Recieved Alarm", Toast.LENGTH_SHORT).show();
        } 
        catch (Exception e) 
        {
            Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

}

In androidmanifest.xml I have this entry before the tag

<receiver  android:name="WeeklyReminder">

I want the reminder to be invoked even when the app is closed. But now nothing is happening
Is it the right way of doing it?
Thanks

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

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

发布评论

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

评论(1

染年凉城似染瑾 2024-12-15 18:46:37

我解决了这个问题,并将其提供给将来可能遇到同样问题的人。
在 androidmanifest 文件中我缺少“.”即我使用的是“WeeklyReminder”而不是“.WeeklyReminder”

所以正确的条目是

<receiver  android:name=".WeeklyReminder">

I resolved the issue and putting it for someone who may come across the same problem in future.
In androidmanifest file I was missing '.' ie instead of ".WeeklyReminder" I was using "WeeklyReminder"

So the correct entry is

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