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

时间:2011-09-21 09:12:10

标签: java android calendar alarm

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

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

    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中声明这个活动?

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

1 个答案:

答案 0 :(得分:2)

尝试在AndroidManifest.xml中添加您的活动“AlarmReceiver”
如果您的活动扩展 Android接收器(例如: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>