北京联合大学实验报告课程(项目)名称:移动互联学院:自动化专业:自动化班级:自控1103B学号:2011100329327姓名:张淑敏成绩:2015年1月18日一、任务与目的设计一个闹钟(计时结束后可设置为提示窗、播放音乐及振动)。1.构建一个定时应用2.定时时间到后可以显示提示框3.同时可以播放指定的音乐4.也可选择音乐与震动一起二、原理(1)闹钟设置:在Android中可以通过AlarmManager来实现闹钟,AlarmManager类是专门用来设定在某个指定的时间去完成指定的事件。AlarmManager提供了访问系统警报的服务,只要在程序中设置了警报服务,AlarmManager就会通过onReceive()方法去执行这些事件,就算系统处于待机状态,同样不会影响运行。可以通过Context.getSystemService方法来获得服务。要实现闹钟,首先需要创建一个继承自BroadcastReceive的类,实现onReceive方法来接收这个Alarm服务,然后通过建立Intent和PendingIntent连接来调用Alarm组件。(2)播放音乐:MediaPlayer类用来播放音频、视频和流媒体,MediaPlayer包含了Audio和Video的播放功能,在Android的界面上,Music和Video两个应用程序都是调用MediaPlayer实现的。任何MediaPlayer对象都必须先处于准备状态,然后才开始播放。(3)震动:开始震动有两个接口1.vibrator.vibrate(2000);//震动指定时间,数据类型long,单位为毫秒,一毫秒为1/1000秒2.vibrator.vibrate(newlong[]{100,10,100,1000},-1);//按照指定的模式去震动。数组参数意义:第一个参数为等待指定时间后开始震动,震动时间为第二个参数。后边的参数依次为等待震动和震动的时间第二个参数为重复次数,-1为不重复,0为一直震动取消震动vibrator.cancel();//取消震动,立即停止震动震动为一直震动的话,如果不取消震动,就算退出,也会一直震动最重要的,增加权限,否则运行时出错uses-permissionandroid:name=android.permission.VIBRATE/三、内容与步骤(1)闹钟设置创建Activity01.java程序:packagecom.yarin.android.Examples_07_07;importjava.util.Calendar;importandroid.app.Activity;importandroid.app.AlarmManager;importandroid.app.PendingIntent;importandroid.app.TimePickerDialog;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;importandroid.widget.TimePicker;publicclassActivity01extendsActivity{ButtonmButton1;ButtonmButton2;TextViewmTextView;Calendarcalendar;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);calendar=Calendar.getInstance();mTextView=(TextView)findViewById(R.id.TextView01);mButton1=(Button)findViewById(R.id.Button01);mButton2=(Button)findViewById(R.id.Button02);mButton1.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){calendar.setTimeInMillis(System.currentTimeMillis());intmHour=calendar.get(Calendar.HOUR_OF_DAY);intmMinute=calendar.get(Calendar.MINUTE);newTimePickerDialog(Activity01.this,newTimePickerDialog.OnTimeSetListener(){publicvoidonTimeSet(TimePickerview,inthourOfDay,intminute){calendar.setTimeInMillis(System.currentTimeMillis());calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);calendar.set(Calendar.MINUTE,minute);calendar.set(Calendar.SECOND,0);calendar.set(Calendar.MILLISECOND,0);/*建立Intent和PendingIntent,来调用目标组件*/Intentintent=newIntent(Activity01.this,AlarmReceiver.class);PendingIntentpendingIntent=PendingIntent.getBroadcast(Activity01.this,0,intent,0);AlarmManageram;/*获取闹钟管理的实例*/am=(AlarmManager)getSystemService(ALARM_SERVICE);/*设置闹钟*/am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);/*设置周期闹*/am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+(10*1000),(24*60*60*1000),pendingIntent);StringtmpS=设置闹钟时间为+format(hourOfDay)+:+format(minute);mTextView.setText(tmpS);}},mHour,mMinute,true).show();}});mButton2.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){Intentintent=newIntent(Activity01.this,AlarmReceiver.class);PendingIntentpendingIntent=PendingIntent.getBroadcast(Activity01.this,0,intent,0);AlarmManageram;/*获取闹钟管理的实例*/am=(AlarmManager)getSystemService(ALARM_SERVICE);/*取消*/am.cancel(pendingIntent);mTextView.setText(闹钟已取消!);}});}/*格式化字符串(7:3-07:03)*/privateStringformat(intx){Strings=+x;if(s.length()==1)s=0+s;returns;}}(2)播放音乐创建Activity02.java程序:packagecom.yarin.android.Examples_07_07;importjava.io.File;importjava.io.FilenameFilter;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importandroid.app.Activity;importandroid.app.AlertDialog;importandroid.app.Dialog;importandroid.app.ListActivity;importandroid.content.DialogInterface;importandroid.media.MediaPlayer;importandroid.media.MediaPlayer.OnCompletionListener;importandroid.os.Bundle;importandroid.view.KeyEvent;importandroid.view.View;importandroid.widget.ArrayAdapter;importandroid.widget.ImageButton;publicclassActivity02extendsActivity{/*MediaPlayer对象*/publicMediaPlayermMediaPlayer=null;protectedDialogmVibrator;/*当前播放歌曲的索引*///privateintcurrentListItme=0;/*音乐的路径*/privatestaticfinalStringMUSIC_PATH=newString(/sdcard/Music/001.mp3);/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);/*构建MediaPlayer对象*/mMediaPlayer=newMediaPlayer();playMusic(MUSIC_PATH);newAlertDialog.Builder(Activity02.this).setTitle(闹钟).setPositiveButton(确定,newDialogInterface.OnClickListener(){@OverridepublicvoidonClick(DialogInterfacearg0,intarg01){//TODOAuto-generatedmethodstubmMediaPlayer.stop();mMediaPlayer.release();mVibrator.cancel();Activity02.this.finish();}}).create().show();;}privatevoidplayMusic(Stringpath){try{/*重置MediaPlayer*/mMediaPlayer.reset();/*设置要播放的文件的路径*/mMediaPlayer.setDataSource(path);/*准备播放*/mMediaPlayer.prepare();/*开始播放*/mMediaPlayer.start();mMediaPlayer.setOnCompletionListener(newOnCompletionListener(){publicvoidonCompletion(MediaPlayerarg