diff --git a/AlarmReceiver.java b/AlarmReceiver.java new file mode 100644 index 0000000..e503aca --- /dev/null +++ b/AlarmReceiver.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.micode.notes.ui; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +public class AlarmReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + intent.setClass(context, AlarmAlertActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); + } +} +package net.micode.notes.ui; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +// AlarmReceiver类继承自BroadcastReceiver,是一个广播接收器,用于接收特定的广播消息并做出相应的响应。 +// 在这个场景下,大概率是接收与闹钟提醒相关的广播,然后启动对应的闹钟提醒界面Activity。 +public class AlarmReceiver extends BroadcastReceiver { + + // onReceive方法是BroadcastReceiver类中最重要的方法,当该广播接收器接收到与之匹配的广播时,此方法会被调用。 + // 它接收两个参数: + // 1. context:表示当前应用的上下文环境,通过它可以访问应用的各种资源、服务等内容。 + // 2. intent:包含了发送广播时所携带的信息,例如额外的数据、动作(Action)等,在这里它可能携带了与闹钟提醒相关的一些标识信息等。 + @Override + public void onReceive(Context context, Intent intent) { + // 通过intent.setClass方法,重新设置该Intent要启动的目标Activity类为AlarmAlertActivity.class。 + // 这意味着当这个Intent被启动时,将会启动AlarmAlertActivity这个界面,通常AlarmAlertActivity就是用于展示闹钟提醒具体内容(如提醒的便签信息、播放提醒声音等)的界面。 + intent.setClass(context, AlarmAlertActivity.class); + + // 为Intent添加FLAG_ACTIVITY_NEW_TASK标志。 + // 这个标志的作用是:如果当前应用处于后台或者没有任何任务栈存在的情况下,添加此标志可以确保新启动的Activity能够在一个新的任务栈中被启动, + // 避免出现因任务栈相关问题而导致Activity启动失败等异常情况,保证闹钟提醒界面能够正常展示给用户。 + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + // 使用传入的context上下文对象,调用startActivity方法来启动经过上述设置后的Intent。 + // 这样就会触发启动AlarmAlertActivity,使得用户可以看到闹钟提醒的相关界面,完成从接收到闹钟提醒广播到展示提醒界面的整个流程。 + context.startActivity(intent); + } +} \ No newline at end of file