gaoxiaorui
gaoxiaorui 2 months ago
parent f7704b28ac
commit 0f977e3717

@ -0,0 +1,197 @@
/*
* 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.app.Activity;// 活动的基类
import android.app.AlertDialog;// 用于创建警告对话框
import android.content.Context; // 提供访问应用程序特定资源的上下文
import android.content.DialogInterface; // 对话框交互接口
import android.content.Intent; // 创建启动新活动的意图
import android.media.AudioManager; // 管理音频设置
import android.media.MediaPlayer; // 播放音频文件
import android.media.RingtoneManager; // 访问系统铃声
import android.net.Uri; // 表示URI
import android.os.Bundle;// 在活动之间传递数据
import android.os.PowerManager; // 管理电源状态
import android.provider.Settings; // 系统设置
import android.view.Window; // 窗口管理
import android.view.WindowManager;// 管理窗口
import net.micode.notes.R; // 导入资源引用
import net.micode.notes.data.Notes; // 导入笔记数据模型
import net.micode.notes.tool.DataUtils; // 数据操作的工具函数
import java.io.IOException; // 处理IO异常
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
private long mNoteId;
// 笔记的唯一标识
private String mSnippet;
// 笔记的内容片段
private static final int SNIPPET_PREW_MAX_LEN = 60;
// 内容片段的最大长度
MediaPlayer mPlayer;
// 媒体播放器,用于播放警报音
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 请求无标题窗口
final Window win = getWindow();
// 设置窗口标志,使其在锁屏时可见
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
// 如果屏幕未亮,则设置相关标志
if (!isScreenOn()) {
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
// 保持屏幕常亮
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
// 打开屏幕
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
// 允许锁定屏幕
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
// 窗口布局插入装饰
}
Intent intent = getIntent();
// 获取启动活动的意图
try {
// 从意图中提取笔记ID
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
// 根据笔记ID获取内容片段
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
// 如果内容片段超过最大长度,则进行截断处理
mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0,
SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info)
: mSnippet;
} catch (IllegalArgumentException e) {
e.printStackTrace(); // 打印异常堆栈信息
return; // 发生异常时返回
}
mPlayer = new MediaPlayer(); // 初始化媒体播放器
// 检查笔记是否在数据库中可见
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
showActionDialog(); // 显示操作对话框
playAlarmSound(); // 播放警报声音
} else {
finish();
// 如果不可见,则结束活动
}
}
// 检查屏幕是否开启的方法
private boolean isScreenOn() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
// 返回屏幕状态
}
}
private void playAlarmSound() {
// 获取默认的闹钟铃声URI
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
// 获取影响静音模式的流类型
int silentModeStreams = Settings.System.getInt(getContentResolver(),
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
// 检查静音模式下是否包括闹钟流
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
mPlayer.setAudioStreamType(silentModeStreams);
// 设置音频流类型为静音模式
} else {
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
// 设置为闹钟流
}
try {
// 设置数据源为铃声URI
mPlayer.setDataSource(this, url);
mPlayer.prepare();
// 准备播放器
mPlayer.setLooping(true);
// 设置循环播放
mPlayer.start();
// 开始播放
} catch (IllegalArgumentException e) {
e.printStackTrace(); // 打印异常信息
} catch (SecurityException e) {
e.printStackTrace(); // 打印异常信息
} catch (IllegalStateException e) {
e.printStackTrace(); // 打印异常信息
} catch (IOException e) {
e.printStackTrace(); // 打印异常信息
}
}
private void showActionDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.app_name);
// 设置对话框标题
dialog.setMessage(mSnippet);
// 设置对话框消息内容
dialog.setPositiveButton(R.string.notealert_ok, this);
// 设置确认按钮
// 如果屏幕已开启,设置取消按钮
if (isScreenOn()) {
dialog.setNegativeButton(R.string.notealert_enter, this);
}
dialog.show().setOnDismissListener(this);
// 显示对话框并设置消失监听器
}
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_NEGATIVE:
// 点击取消按钮时,启动编辑笔记活动
Intent intent = new Intent(this, NoteEditActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_UID, mNoteId);
// 传递笔记ID
startActivity(intent);
// 启动活动
break;
default:
break; // 默认情况不处理
}
}
public void onDismiss(DialogInterface dialog) {
stopAlarmSound();
// 停止播放警报声音
finish();
// 结束当前活动
}
private void stopAlarmSound() {
if (mPlayer != null) {
mPlayer.stop();
// 停止播放
mPlayer.release();
// 释放媒体播放器资源
mPlayer = null;
// 清空播放器引用
}
}

@ -0,0 +1,81 @@
/*
* 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;
// 导入所需的Android类
import android.app.AlarmManager; // 用于管理闹钟服务
import android.app.PendingIntent; // 用于延迟执行的意图
import android.content.BroadcastReceiver; // 广播接收器基类
import android.content.ContentUris; // 用于处理内容URI
import android.content.Context; // 上下文类
import android.content.Intent; // 意图类
import android.database.Cursor; // 数据库结果集游标
// 导入笔记相关的数据类
import net.micode.notes.data.Notes; // 笔记数据类
import net.micode.notes.data.Notes.NoteColumns; // 笔记列定义类
public class AlarmInitReceiver extends BroadcastReceiver {
// 定义要查询的列
private static final String [] PROJECTION = new String [] {
NoteColumns.ID,
// 笔记ID
NoteColumns.ALERTED_DATE
// 警报日期
};
// 列索引
private static final int COLUMN_ID = 0;
// ID列索引
private static final int COLUMN_ALERTED_DATE = 1;
// 警报日期列索引
@Override
public void onReceive(Context context, Intent intent) {
long currentDate = System.currentTimeMillis(); // 获取当前时间戳
// 查询提醒日期大于当前时间的笔记
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION,
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
new String[] { String.valueOf(currentDate) },
null);
if (c != null) {
// 如果结果集不为空
if (c.moveToFirst()) {
do {
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
// 获取警报日期
Intent sender = new Intent(context, AlarmReceiver.class); // 创建意图,用于发送警报
// 设置数据为对应的笔记URI
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 创建PendingIntent注册广播接收器
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 获取AlarmManager服务
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// 设置闹钟
alarmManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext());
// 遍历结果集
}
c.close();
// 关闭Cursor
}
}
}

@ -0,0 +1,36 @@
/*
* 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; // 导入意图类
// 定义AlarmReceiver类继承自BroadcastReceiver
public class AlarmReceiver extends BroadcastReceiver {
@Override
// 当接收到广播时调用的方法
public void onReceive(Context context, Intent intent) {
// 设置要启动的活动为AlarmAlertActivity
intent.setClass(context, AlarmAlertActivity.class);
// 添加标志,使新的活动在新的任务中启动
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 启动AlarmAlertActivity
context.startActivity(intent);
}
}
Loading…
Cancel
Save