创建日历事件时如何创建警报?
我已在日历中插入一个事件。创建事件的时间也必须与同时响铃的警报一起创建。怎么做呢?我使用了以下代码,它给出了以下错误。
当我使用以下代码时出现以下错误: 主要活动:
Calendar caln = Calendar.getInstance();
caln.add(Calendar.SECOND, 2);
Intent intent = new Intent(ToDoApplicationActivity.this, AlarmReceiver.class);
intent.putExtra("alarm_message", title1);
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
startActivity(intent);
OnReceive 方法被覆盖:
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Intent newIntent = new Intent(context, ToDoApplicationActivity.class);
newIntent.putExtra("alarm_message", message);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
错误得到:
android.content.ActivityNotFoundException:无法找到显式活动类 {com.android.todoapplication/android.todoapplication.AlarmReceiver};您是否在 AndroidManifest.xml 中声明了此活动?
非常感谢任何帮助,并提前致谢...
I have inserted an event in a calendar. The time of event created must also be created with alarm of the same time to ring. How to do it? I have used following code and it gives folowing error.
I get following error when i use the following code:
Main Activity:
Calendar caln = Calendar.getInstance();
caln.add(Calendar.SECOND, 2);
Intent intent = new Intent(ToDoApplicationActivity.this, AlarmReceiver.class);
intent.putExtra("alarm_message", title1);
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
startActivity(intent);
OnReceive method overrided:
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Intent newIntent = new Intent(context, ToDoApplicationActivity.class);
newIntent.putExtra("alarm_message", message);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
ERROR got:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.todoapplication/android.todoapplication.AlarmReceiver}; have you declared this activity in your AndroidManifest.xml?
Any Help is highly appreciated and thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在
AndroidManifest.xml
中添加您的活动“AlarmReceiver”。如果您的活动扩展 Android 接收器(例如:BroadcastReceiver),请按如下方式添加:
else
Try to add your activity "AlarmReceiver" in your
AndroidManifest.xml
.If your activity extends an Android Receiver (E.g: BroadcastReceiver), Add it like this :
else