Java 如何在创建日历事件时创建报警?
我已在日历中插入事件。创建事件的时间也必须与发出警报的时间相同。怎么做?我使用了下面的代码,它给出了以下错误 当我使用以下代码时,出现以下错误: 主要活动:Java 如何在创建日历事件时创建报警?,java,android,calendar,alarm,Java,Android,Calendar,Alarm,我已在日历中插入事件。创建事件的时间也必须与发出警报的时间相同。怎么做?我使用了下面的代码,它给出了以下错误 当我使用以下代码时,出现以下错误: 主要活动: Calendar caln = Calendar.getInstance(); caln.add(Calendar.SECOND, 2); Intent intent = new Intent(ToDoApplicationActivity.this, AlarmReceiver.class); inten
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中声明了此活动
非常感谢您提供的任何帮助…请尝试在您的
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>