在 Android 应用程序中创建提醒
我正在尝试在我的应用程序中创建每周提醒。为此,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了这个问题,并将其提供给将来可能遇到同样问题的人。
在 androidmanifest 文件中我缺少“.”即我使用的是“WeeklyReminder”而不是“.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