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.
123456/java/net/micode/notes/ui/AlarmReceiver.java

40 lines
2.5 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.
* 总体分析
这段 Java 程序定义了一个名为AlarmReceiver的广播接收者类其主要功能是在接收到相应广播后进行一系列操作来启动一个新的ActivityAlarmAlertActivity。通过设置相关的Intent标志位以及指定目标Activity类利用Context的startActivity方法来启动该Activity整体在安卓系统的广播机制中起到了从接收到广播到触发对应界面显示的衔接作用从而可以基于之前设置的闹钟提醒等相关广播触发逻辑展示出提醒用户的具体界面由AlarmAlertActivity负责呈现
函数分析
onReceive函数
所属类AlarmReceiver继承自BroadcastReceiver专门用于处理接收到的广播消息。
功能:
首先使用intent.setClass方法对传入的Intent进行设置将其目标类指定为AlarmAlertActivity也就是告诉系统接下来要启动的是这个Activity以此来确定具体要展示的界面内容。
接着通过intent.addFlags方法给Intent添加FLAG_ACTIVITY_NEW_TASK标志位这个标志位的作用是让启动的Activity在新的任务栈中进行创建和运行这对于广播接收者启动Activity的场景来说是很有必要的确保Activity能正确启动并展示给用户避免一些因任务栈相关问题导致的启动异常等情况。
最后调用context.startActivity方法传入处理后的Intent从而触发启动AlarmAlertActivity完成从接收到广播到展示对应提醒界面的完整流程。
*/
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);
}
}