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.
123/AlarmAlertActivity.java

210 lines
8.0 KiB

3 months ago
/*
* 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.
*/
1 month ago
package net.micode.notes.ui;//定义一个名为net.micode.notes.ui的包
3 months ago
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;
1 month ago
3 months ago
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
1 month ago
private long mNoteId; //文本在数据库存储中的ID号
private String mSnippet;//闹钟提示时出现的文本片段
3 months ago
private static final int SNIPPET_PREW_MAX_LEN = 60;
MediaPlayer mPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
1 month ago
//Bundle类型的数据与Map类型的数据相似都是以key-value的形式存储数据的
//onsaveInstanceState方法是用来保存Activity的状态的
//能从onCreate的参数savedInsanceState中获得状态数据
3 months ago
requestWindowFeature(Window.FEATURE_NO_TITLE);
1 month ago
//界面显示——无标题
3 months ago
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
1 month ago
3 months ago
if (!isScreenOn()) {
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
1 month ago
//保持窗体点亮
3 months ago
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
1 month ago
//将窗体点亮
3 months ago
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
1 month ago
//允许窗体点亮时锁屏
3 months ago
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
1 month ago
}//在手机锁屏后如果到了闹钟提示时间,点亮屏幕
3 months ago
Intent intent = getIntent();
try {
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
1 month ago
//根据ID从数据库中获取标签的内容
//getContentResolver是实现数据共享实例存储。
3 months ago
mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0,
SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info)
: mSnippet;
1 month ago
//判断标签片段是否达到符合长度
3 months ago
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
}
1 month ago
/*
try
{
// 代码区
}
catch(Exception e)
{
// 异常处理
}
*/
3 months ago
mPlayer = new MediaPlayer();
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
showActionDialog();
1 month ago
//弹出对话框
3 months ago
playAlarmSound();
1 month ago
//闹钟提示音激发
3 months ago
} else {
finish();
1 month ago
//完成闹钟动作
3 months ago
}
}
1 month ago
3 months ago
private boolean isScreenOn() {
1 month ago
//判断屏幕是否锁屏,调用系统函数判断,最后返回值是布尔类型
3 months ago
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
}
1 month ago
3 months ago
private void playAlarmSound() {
1 month ago
//闹钟提示音激发
3 months ago
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
1 month ago
//调用系统的铃声管理URI得到闹钟提示音
3 months ago
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 {
mPlayer.setDataSource(this, url);
1 month ago
//方法setDataSource(Context context, Uri uri)
//解释:无返回值,设置多媒体数据来源【根据 Uri】
3 months ago
mPlayer.prepare();
1 month ago
//准备同步
3 months ago
mPlayer.setLooping(true);
1 month ago
//设置是否循环播放
3 months ago
mPlayer.start();
1 month ago
//开始播放
3 months ago
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
1 month ago
//e.printStackTrace()函数功能是抛出异常, 还将显示出更深的调用信息
//System.out.println(e),这个方法打印出异常,并且输出在哪里出现的异常
3 months ago
} 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();
}
}
1 month ago
3 months ago
private void showActionDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
1 month ago
//AlertDialog的构造方法全部是Protected的
//所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。
//要创建一个AlertDialog就要用到AlertDialog.Builder中的create()方法
//如这里的dialog就是新建了一个AlertDialog
3 months ago
dialog.setTitle(R.string.app_name);
1 month ago
//为对话框设置标题
3 months ago
dialog.setMessage(mSnippet);
1 month ago
//为对话框设置内容
3 months ago
dialog.setPositiveButton(R.string.notealert_ok, this);
1 month ago
//给对话框添加"Yes"按钮
3 months ago
if (isScreenOn()) {
dialog.setNegativeButton(R.string.notealert_enter, this);
1 month ago
}//对话框添加"No"按钮
3 months ago
dialog.show().setOnDismissListener(this);
}
1 month ago
3 months ago
public void onClick(DialogInterface dialog, int which) {
1 month ago
switch (which) {
//用which来选择click后下一步的操作
3 months ago
case DialogInterface.BUTTON_NEGATIVE:
1 month ago
//这是取消操作
3 months ago
Intent intent = new Intent(this, NoteEditActivity.class);
1 month ago
//实现两个类间的数据传输
3 months ago
intent.setAction(Intent.ACTION_VIEW);
1 month ago
//设置动作属性
3 months ago
intent.putExtra(Intent.EXTRA_UID, mNoteId);
1 month ago
//实现key-value对
//EXTRA_UID为keymNoteId为键
3 months ago
startActivity(intent);
1 month ago
//开始动作
3 months ago
break;
default:
1 month ago
//这是确定操作
3 months ago
break;
}
}
1 month ago
3 months ago
public void onDismiss(DialogInterface dialog) {
1 month ago
//忽略
3 months ago
stopAlarmSound();
1 month ago
//停止闹钟声音
3 months ago
finish();
1 month ago
//完成该动作
3 months ago
}
1 month ago
3 months ago
private void stopAlarmSound() {
if (mPlayer != null) {
mPlayer.stop();
1 month ago
//停止播放
3 months ago
mPlayer.release();
1 month ago
//释放MediaPlayer对象
3 months ago
mPlayer = null;
}
}
}