|
|
/*
|
|
|
* 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的广播接收者类,其主要功能是在接收到相应广播后,进行一系列操作来启动一个新的Activity(AlarmAlertActivity)。通过设置相关的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);
|
|
|
}
|
|
|
}
|