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

发布于 2024-12-05 19:50:44 字数 1464 浏览 0 评论 0原文

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

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

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

颜漓半夏 2024-12-12 19:50:44

尝试在 AndroidManifest.xml 中添加您的活动“AlarmReceiver”
如果您的活动扩展 Android 接收器(例如:BroadcastReceiver),请按如下方式添加:

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

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

else

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

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

Try to add your activity "AlarmReceiver" in your AndroidManifest.xml.
If your activity extends an Android Receiver (E.g: BroadcastReceiver), Add it like this :

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

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

else

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

  <activity android:name=".AlarmReceiver"/>
</application>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文