生活编程(三) 简单的午睡闹钟

来源:互联网 发布:圣思园张龙java视频 编辑:程序博客网 时间:2024/04/25 03:04

    高手绕道,东西简单。

   

    没发现有有AlarmManager的存在,不过也罢,用一下线程,震动和多媒体还是能解决午睡问题。午睡的时候不定时,每次都要调时间,其实我只要睡个15分钟,以前的天才生跟我讲的,午睡十五分钟相当于晚上的几个小时。


                                            

                                                                                                  简简单单的界面。

    震动器(Vibrator)在android.os,以前是一直震,可以调成像来电一样有频率地震动。 官文:

public abstract void vibrate(long[] pattern, int repeat)

Since: API Level 1

Vibrate with a given pattern.

Pass in an array of ints that are the durations for which to turn on or off the vibrator in milliseconds. The first value indicates the number of milliseconds to wait before turning the vibrator on. The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off. Subsequent values alternate between durations in milliseconds to turn the vibrator off or to turn the vibrator on.

To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.

This method requires the caller to hold the permission VIBRATE.

Parameters
patternan array of longs of times for which to turn the vibrator on or off.repeatthe index into pattern at which to repeat, or -1 if you don't want to repeat.

Vibrator vi.vibrate(new long[]{1000,1000,1000,1000},0);
0表示一直维持这个周期,1秒停,1秒震,再1秒停,再震。-1就是一周期。


AlarmActivity 类:

package com.works.iaiti;import java.util.Calendar;import com.example.alarm.R;import android.media.AudioManager;import android.media.MediaPlayer;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import android.app.Service;import android.app.AlertDialog.Builder;import android.app.Dialog;import android.content.DialogInterface;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;import android.support.v4.app.NavUtils;public class AlarmActivity extends Activity {AlarmThread alarmthread;MediaPlayer mp;        AudioManager am;int ahour;int aminute;int asecond;int longtime = 20;String title ="午睡闹钟";String content = "20分钟后响铃?";int count = 0;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_alarm);                Calendar cl = Calendar.getInstance();        //System.out.println(cl.getTime());        Button date_btn = (Button)findViewById(R.id.Button01);        date_btn.setOnClickListener(new OnClickListener() {public void onClick(View v) {        count ++;// 防止启动两次        if(count == 1){            // 计算响铃时间,传入构造函数中                                Calendar cl = Calendar.getInstance();    int ahour = cl.get(Calendar.HOUR);    int aminute = cl.get(Calendar.MINUTE)+longtime;    int asecond = cl.get(Calendar.SECOND);        //线程启动    alarmthread = new AlarmThread(AlarmActivity.this, ahour, aminute, asecond);    alarmthread.start();                                Toast.makeText(AlarmActivity.this,"闹钟启动,20分钟后响铃", Toast.LENGTH_SHORT).show();        }         else {            Toast.makeText(AlarmActivity.this,"闹钟已经启动,取消请按返回键", Toast.LENGTH_SHORT).show();        }}});    } // 返回键 弹出框  及音量控制    public boolean onKeyDown(int keycode, KeyEvent keyevent) {            am = (AudioManager) getSystemService(Service.AUDIO_SERVICE);            //返回键             if (keycode == 4) {                    //对话框弹出 让用户选择                    Builder b = new AlertDialog.Builder(AlarmActivity.this);                    b.setTitle("— —! 你不睡觉了吗");                    b.setMessage("你真确定关闭闹钟");                    b.setPositiveButton("确定", new DialogInterface.OnClickListener() {                            public void onClick(DialogInterface dialog, int whichButton) {                                    Toast.makeText(AlarmActivity.this, "byebye ",                                                    Toast.LENGTH_SHORT).show();                                    //退出 停止闹钟 关闭震动和播放器                                    alarmthread.stopAlarm();                                   finish();                            }                    })                    //链式表达式                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {                            public void onClick(DialogInterface dialog, int whichButton) {                            }                    }).create().show();                    return true;            }            //音量控制            else if (keycode == KeyEvent.KEYCODE_VOLUME_DOWN) {                    am.adjustStreamVolume(AudioManager.STREAM_MUSIC,                                    AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);                    return true;            }                        if (keycode == KeyEvent.KEYCODE_VOLUME_UP) {                    am.adjustStreamVolume(AudioManager.STREAM_MUSIC,                                    AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);                    return true;            }            else              return super.onKeyDown(keycode, keyevent);         }}

包括入睡,加上去大概20分钟左右即可。


AlarmThread 类:

package com.works.iaiti;import java.io.IOException;import java.util.Calendar;import com.example.alarm.R;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.media.MediaPlayer;import android.os.Vibrator;import android.widget.Toast;public class AlarmThread extends Thread{AlarmActivity alarmactivity;Vibrator vi ;MediaPlayer mp;boolean forever = true;int hour;int minute;boolean tag = true;int second;public AlarmThread(AlarmActivity alarmactivity,int hour,int minute,int second){this.alarmactivity = alarmactivity;this.hour = hour;this.minute = minute;this.second = second;}@Overridepublic void run() {while(forever){Calendar c = Calendar.getInstance();int shour = c.get(Calendar.HOUR);int sminute = c.get(Calendar.MINUTE);int ssecond = c.get(Calendar.SECOND);if(this.hour== shour&& this.minute == sminute && this.second == ssecond){        //媒体播放器的启动 放音乐        mp = MediaPlayer.create(alarmactivity, R.raw.alarm);                try {                        mp.prepare();                } catch (IllegalStateException e) {                        e.printStackTrace();                } catch (IOException e) {                        e.printStackTrace();                }                mp.start();                                //震动器 启动vi = (Vibrator)alarmactivity.getSystemService(alarmactivity.VIBRATOR_SERVICE);vi.vibrate(new long[]{1000,1000,10000,1000},0);mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {                        /* 覆盖文件播出完毕事件 */                        public void onCompletion(MediaPlayer arg0) {                                try {                                        mp.release();                                } catch (Exception e) {                                        e.printStackTrace();                                }                        }                });forever = false;try{Thread.sleep(1000);}catch(Exception e){e.printStackTrace();}}}}//关闭public  void stopAlarm(){    vi.cancel();    mp.stop();}}

可以了 ,这样就不用每次调时间了,每次躺下按一下就可以睡午觉了。


2014-4-8 15:30:11

bug发现。

写的代码不严谨,后面调试发现空指针错误,如果还没到时间就退出,线程是没有new的,此时调用线程的方法肯定空指针了。

还有震动和媒体播放在没到时间也是不实例化的,所以添加一个boolean判断闹钟响起没。

同时,发现,自己少弄了服务的东西,一旦屏幕关闭就出现了线程挂起的问题,闹钟不叫了,迟到了。

这屏幕不关耗电太大了,回头写个新的。


3 0
原创粉丝点击