/* * 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; public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {//单击,关闭时操作 private long mNoteId; //文本在数据库存储中的ID号 private String mSnippet;//闹钟提示时出现的文本片段 private static final int SNIPPET_PREW_MAX_LEN = 60;//闹钟最大持续时间 MediaPlayer mPlayer; @Override protected void onCreate(Bundle savedInstanceState) {//onsaveInstanceState方法是用来保存Activity的状态的 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 { 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 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 { mPlayer.setDataSource(this, url);//设置多媒体数据来源 mPlayer.prepare();//准备同步 mPlayer.setLooping(true);//设置是否循环播放 mPlayer.start();//播放开始 } catch (IllegalArgumentException e) {//跑车异常 // 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);//为对话框设置标题 dialog.setMessage(mSnippet);//为对话框设置内容 dialog.setPositiveButton(R.string.notealert_ok, this);//yes按钮的设置 if (isScreenOn()) { dialog.setNegativeButton(R.string.notealert_enter, this);//no按钮的设置 } dialog.show().setOnDismissListener(this); } public void onClick(DialogInterface dialog, int which) { switch (which) {//用which来选择click后下一步的操作 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; } } }