/* * 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) { 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);//根据ID获取标签的内容,利用getContentResolver()可以得到当前应用的实例 /*判断标签是否达到符合的长度*/ 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这个类来获取数据库中的数据 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) { //传递了一个不合法或不正确的参数时出现异常 // 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); //设置确定按钮 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对象 intent.setAction(Intent.ACTION_VIEW); //调用隐式intent调用 intent.putExtra(Intent.EXTRA_UID, mNoteId); //Intent.EXTRA_UID为键名,mNoteId为键对应的值,把想要传递的数据暂存在intent中,当另一个活动启动后,再把这些数据从intent缓存中取出 startActivity(intent); //开始跳转 break; default: break; //确定 } } public void onDismiss(DialogInterface dialog) { stopAlarmSound(); //停止闹钟声音 finish(); //完成动作 } private void stopAlarmSound() { if (mPlayer != null) { mPlayer.stop(); //停止播放 mPlayer.release(); //释放对象 mPlayer = null; } } }