java – 如何在创建日历事件时创建警报?

我在日历中插入了一个事件.创建事件的时间也必须创建,同时响铃以响铃.怎么做?我使用了以下代码,它给出了以下错误.

当我使用以下代码时出现以下错误:
主要活动:

    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: Unable to find explicit activity class {com.android.todoapplication/android.todoapplication.AlarmReceiver}; have you declared this activity in your AndroidManifest.xml?

任何帮助都非常感谢,并提前感谢…

最佳答案 尝试在AndroidManifest.xml中添加您的活动“AlarmReceiver”.

如果您的活动扩展了Android Receiver(例如:BroadcastReceiver),请按以下方式添加:

<application android:label="@string/app_name" ...>
  ...

  <receiver android:name=".AlarmReceiver" android:process=":remote" />
</application>

其他

<application android:label="@string/app_name" ...>
  ...

  <activity android:name=".AlarmReceiver"/>
</application>
点赞