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.
gitProject/src/main/java/net/micode/notes/ui/AlarmAlertActivity.java

162 lines
8.7 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.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.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;//import iiii//im
//import 各种文件
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
//6. 该类继承于Activity类并实现了OnClickListener, OnDismissListener接口主要实现的功能是对相关点击事件的侦听里面实现了创建AlarmAlert的过程在这个过程中需要检查该闹铃是否显示即isScreenOn方法并实现了对闹铃声音的控制play以及stop两种方法实现了对闹铃的控制过程
// OnClick, OnDisMiss方法实现了对闹铃设置相关数据的设置管理。对于停止闹铃的实现主要是对mPlayer数据的stop以及release实现。
private long mNoteId; //定义一个关于便签 的id号
private String mSnippet;//闹钟提示时出现的字符串
private static final int SNIPPET_PREW_MAX_LEN = 60; //定义字符串长度的最大值
MediaPlayer mPlayer; // 定义一个媒体播放器
@Override//
protected void onCreate(Bundle savedInstanceState) { //重载onCreate的方法
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//要隐藏标题栏
final Window win = getWindow(); //获取窗口实例由于上一行已经设置为无窗口类型故final表示窗口已经无法改变
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); // 锁屏时出现的提示字符串
if (!isScreenOn()) { //定义一个1.定义了一个当前闹钟提醒窗口的临时变量,通过它为闹钟提醒窗口添加一项Flag即将其设置成在能够锁屏界面上显示的窗口。
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 { //try 里面的内容如果出现错误或者是没有该变量也不会卡死程序会跳过try继续运行
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));//通过intent临时变量获取事先存入的标签的id号存入mNoteId
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);//通过该id号从DataUtils中获取所对应便签的摘要存入mSnippet中
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;
} //catch 捕获IllegalArgumentException e异常若出现异常则输出
mPlayer = new MediaPlayer(); //新建一个媒体播放器
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
showActionDialog(); //由该便签的id从数据库中获取其类型判断是否为需要提醒的便签若是该类型便签则显示对话框
playAlarmSound(); //开始播放闹钟铃声
} else {
finish(); //否则闹钟结束
}
}
private boolean isScreenOn() { //布尔函数 判断屏幕是否锁屏
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn(); // 1.创建android类PowerManager的一个实例对象通过android函数getSystemService函数获取
// 当前android系统的电源管理句柄
}
private void playAlarmSound() { //播放闹钟提示音
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
//使用了android的铃声管理器方法获取当前默认的系统提醒铃声的Uri并存入变量url中
int silentModeStreams = Settings.System.getInt(getContentResolver(),
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
//setting.system获取全局变量在此处是判断手机是否静音
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
mPlayer.setAudioStreamType(silentModeStreams); //根据当前的设置是否为静音状态和当前的系统铃声来指定流媒体类型。
} else {
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
} //是媒体播放器发出警报
try { //使用相关的的闹钟信息使用try避免异常
mPlayer.setDataSource(this, url);
mPlayer.prepare(); //进行准备工作
mPlayer.setLooping(true); // 设置闹钟循环
mPlayer.start(); //播放开始
} catch (IllegalArgumentException e) {/*使用catch来捕获异常获取非法异常参数*/
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) { //安全异常
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) { //非法状态异常
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) { // 输出异常
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void showActionDialog() { //显示操作对话框
AlertDialog.Builder dialog = new AlertDialog.Builder(this); // new对话框
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);
} /*显示设定好的提醒对话框并附加一个解除监听器在对话框解除时可执行onDismiss的功能*/
public void onClick(DialogInterface dialog, int which) { //不同的switch来确定接下来的操作
switch (which) {
case DialogInterface.BUTTON_NEGATIVE: //进行取消操作
Intent intent = new Intent(this, NoteEditActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_UID, mNoteId);
startActivity(intent); //开始活动
break;
default:
break;
}
}
public void onDismiss(DialogInterface dialog) { /*对话框取消时停止提醒*/
stopAlarmSound();//结束闹钟铃声
finish();
}
private void stopAlarmSound() {/*闹钟结束操作*/
if (mPlayer != null) { //如果播放器的提示框仍存在
mPlayer.stop(); //停止闹钟播放器
mPlayer.release(); //释放播放器
mPlayer = null; ///清空播放器内容
}
}
}