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

176 lines
8.4 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;
/*该类继承自Activity类
功能是实现了OnClickListener和OnDismissListener接口。
*/
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
/*
功能首先设置窗口的特性使得该Activity在锁屏状态下也能显示。
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//继承父类的oncreate执行父类方法并获取由父类保存的activity状态信息
requestWindowFeature(Window.FEATURE_NO_TITLE);//定义activity活动的界面情况这里是隐藏标题栏
final Window win = getWindow();//用win变量获取当前窗口
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);//设定当前窗口标签,设置成锁定在屏幕上方。
//通过修改 WindowManager.LayoutParams 的属性,可以实现对窗口的自定义布局和交互行为。
if (!isScreenOn()) {//使用后边定义的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();//intent中存的是这个活动的“意图”这个数据类型可以在不同的Activity之间传递信息。
try {
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));//intent中的data是指URL数据他定义了执行这个Activity所需要的数据将URL解码取第一个也即Note的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) {//使用catch这个方式来捕捉try中出现的报错如果上述过程中发生了这个异常“当方法接收到一个非法或不合理的参数时”就把异常情况打印出来
e.printStackTrace();
return;
}
mPlayer = new MediaPlayer(); //创建一个新的多媒体变量传给mPlayer
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {//只有普通笔记才会有通知
showActionDialog();//打开操作对话框
playAlarmSound();//打开播放提示音
} else {
finish();
}
}
/*
功能用于判断当前屏幕是否处于开启状态使用已经写好的PowerManger对象和
*/
private boolean isScreenOn() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); //使用 getSystemService获取系统服务指定为获取电源状态
return pm.isScreenOn(); //使用自带的isScreenOn函数判断屏幕状态
}
/*
功能:在物理层面上实现播放通知铃声的功能
*/
private void playAlarmSound() {
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);//获取默认铃声,是闹钟铃声
int silentModeStreams = Settings.System.getInt(getContentResolver(),//获取静音模式下影响音频流的类型如果没有MODE_RINGER_STREAMS_AFFECTED一项也即不处于静音模式的话返回0
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {//如果结果匹配,则将闹钟声音的音频设置成静音模式
mPlayer.setAudioStreamType(silentModeStreams);
} else {
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);//不是静音模式的话就正常设置成闹钟音频流
}
try {
mPlayer.setDataSource(this, url);//准备设备将音频源设成url
mPlayer.prepare();//准备设备
mPlayer.setLooping(true);//设置循环
mPlayer.start();//开始播放
} catch (IllegalArgumentException e) {//try的过程中随时报错这里有四种错误这个是参数传递出错
// 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);//创建一个新的对话框对象
dialog.setTitle(R.string.app_name);//设置标题是app的名字
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);
startActivity(intent);
break;
default:
break;
}
}
/*
功能:关闭这个对话框的话,就关闭声音并且结束对话框
*/
public void onDismiss(DialogInterface dialog) {
stopAlarmSound();
finish();
}
/*
功能:实现关闭铃声的物理层操作
*/
private void stopAlarmSound() {
if (mPlayer != null) {//如果声音还在播的话就。。。
mPlayer.stop();//停止声音
mPlayer.release();//释放空间
mPlayer = null;//设置成没有声音。
}
}
}