You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
note/AlarmReceiver.java

60 lines
3.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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);
}
}