【发布时间】:2025-12-08 04:55:01
【问题描述】:
我在日历中插入了一个事件。创建事件的时间也必须与创建时间相同的闹钟响铃。怎么做?我使用了以下代码,它给出了以下错误。
当我使用以下代码时出现以下错误: 主要活动:
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 中声明了此活动?
任何帮助都非常感谢,并在此先感谢...
【问题讨论】:
标签: java android calendar alarm