当前位置:网站首页>“山东大学移动互联网开发技术教学网站建设”项目实训日志四
“山东大学移动互联网开发技术教学网站建设”项目实训日志四
2022-07-29 05:19:00 【BEER_7】
时间:
21春季学期第八周
个人工作内容:
完成摇摇闹钟案例app
详细记录:
- 指南针案例app设计说明
- 目标用户:android开发入门阶段的学生。
- 需求说明:说明加速度传感器的使用方法,讲解AlarmManager闹钟服务基础,讲解activity间信息传递,还有播放背景音乐、震动。
- 设计说明:用户可以自己设置闹钟,到点后闹钟响铃并震动,需要摇晃手机30次才能关闭闹钟。
要点说明:
页面绘制、细节判断等内容将略过,只简述要点。
- 首页
对“07:00”TextView设置点击事件,调转到子activity(设置闹钟时间):
thetime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(MainActivity.this, selecttime.class);
// startActivity(intent1);
startActivityForResult(intent1,10);
}
});
- 设置闹钟时间
xml文件中TimePicker组件:
<TimePicker
android:id="@+id/ttime"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
activity中监听TimePicker取值变化:
//设置是否24小时制显示
mTimepicker.setIs24HourView(true);
//禁用键盘输入
mTimepicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
mTimepicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// Toast.makeText(selecttime.this,hourOfDay+"时"+minute+"分",Toast.LENGTH_SHORT).show();
thehour = hourOfDay;
themin = minute;
}
});
至此可以获取到用户选择的闹钟时间,下一步是将时间传递给首页的activity
- 父子activity数据传递
主页activity中代码(同时注意改变主页时间TextView):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(10 == requestCode){
Bundle bundle=data.getExtras();
if(bundle.getInt("flag") == 1){
cgdhour = bundle.getInt("hour");
cgdmin = bundle.getInt("minute");}}}
子activity中代码:
changetm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
Bundle bundle=new Bundle();
bundle.putInt("flag", 1);
bundle.putInt("hour", thehour);
bundle.putInt("minute", themin);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
});
- AlarmManager闹钟服务
am = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
intent = new Intent(MainActivity.this, ring.class);
pendingIntent = PendingIntent.getActivity(this,0,intent,0);
对Switch组件设置监听:
thebtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
if(isoutdue(cgdhour,cgdmin) == 1)
Toast.makeText(MainActivity.this,"闹钟已开启,将于明日"+thetime.getText()+"响铃",Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this,"闹钟已开启,将于今日"+thetime.getText()+"响铃",Toast.LENGTH_SHORT).show();
setAlarm();
}else {
Toast.makeText(MainActivity.this,"闹钟已关闭",Toast.LENGTH_SHORT).show();
cancelAlarm();
}
}
});
开启闹钟:
private void setAlarm(){
//android Api的改变不同版本中设置有所不同
if(Build.VERSION.SDK_INT<19){
am.set(AlarmManager.RTC_WAKEUP,getTimeDiff(cgdhour,cgdmin),pendingIntent);
}else{
am.setExact(AlarmManager.RTC_WAKEUP,getTimeDiff(cgdhour,cgdmin),pendingIntent);
}
}
关闭闹钟:
private void cancelAlarm(){ am.cancel(pendingIntent); }
获取set(int type,long startTime,PendingIntent pi)中参数startTime:
public long getTimeDiff(int a, int b){
//获得Calendar这个类的实例
Calendar ca=Calendar.getInstance();
//注意精确到秒,时间可以自由设置
//设置为明日
if(isoutdue(a,b) == 1){
ca.set(Calendar.DATE,ca.get(Calendar.DATE)+1);
ca.set(Calendar.HOUR_OF_DAY,a);
ca.set(Calendar.MINUTE,b);
ca.set(Calendar.SECOND,0);
}
else {
ca.set(Calendar.HOUR_OF_DAY,a);
ca.set(Calendar.MINUTE,b);
ca.set(Calendar.SECOND,0);
}
return ca.getTimeInMillis();
}
- 响铃页面
设置背景音乐:
intent = new Intent(ring.this, MyIntentService.class);
String action = MyIntentService.ACTION_MUSIC;
intent.setAction(action);
startService(intent);
震动:
vibrator = (Vibrator)this.getSystemService(this.VIBRATOR_SERVICE);
long[] patter = {0, 1000, 0, 1000};
vibrator.vibrate(patter, 1);
加速度传感器相关设置:
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
继承SensorEventListener接口时需要实现方法:
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
int sensorType = event.sensor.getType();
//values[0]:X轴,values[1]:Y轴,values[2]:Z轴
float[] values = event.values;
if(sensorType == Sensor.TYPE_ACCELEROMETER){
/*因为一般正常情况下,任意轴数值最大就在9.8~10之间,只有在你突然摇动手机
*的时候,瞬时加速度才会突然增大或减少。
*所以,经过实际测试,只需监听任一轴的加速度大于14的时候,改变你需要的设置就OK了~~~
*/
if((Math.abs(values[0])>14||Math.abs(values[1])>14||Math.abs(values[2])>14)){
//事件
i--;
changei();
if(i == 0)
//摇晃完成,关闭页面,同时关闭震动和背景音乐
finish();
}
}
}
后期工作规划:
- 下周开始教学网站的建设
边栏推荐
- Basic concepts of MySQL + database system structure + extended application + basic command learning
- MySQL decompressed version windows installation
- “山东大学移动互联网开发技术教学网站建设”项目实训日志六
- 运动健康深入人心,MOVE PROTOCOL引领品质生活
- 解决表单校验提示信息不消失问题以及赋值不生效问题
- Hcia-r & s self use notes (24) ACL
- 与多家机构战略合作,背后彰显PlatoFarm元宇宙龙头的实力
- WIN10 编译ffmpeg(包含ffplay)
- [sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function
- Win10 compiles ffmpeg (including ffplay)
猜你喜欢
The Platonic metauniverse advocated by musk has long been verified by platofarm
Solve the problem that the prompt information of form verification does not disappear and the assignment does not take effect
365 day challenge leetcode1000 question - day 036 binary tree pruning + subarray and sorted interval sum + delete the shortest subarray to order the remaining arrays
我的理想工作,码农的绝对自由支配才是最重要的——未来创业的追求
华为2020校招笔试编程题 看这篇就够了(下)
与多家机构战略合作,背后彰显PlatoFarm元宇宙龙头的实力
Starfish OS:以现实为纽带,打造元宇宙新范式
HCIA-R&S自用笔记(27)综合实验
实现table某个单元格背景色设置
量化开发必掌握的30个知识点【什么是分笔逐笔数据】?
随机推荐
The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey
[sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function
DAY6:利用 PHP 编写文件上传页面
JS simple code determines whether the device that opens the page is the PC end of the computer, the H5 end of the mobile phone, or the wechat end
QPalette学习笔记
实现table某个单元格背景色设置
[C language series] - storage of deep anatomical data in memory (II) - floating point type
OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程
Win10 compiles ffmpeg (including ffplay)
[C language series] - string + partial escape character explanation + annotation tips
Qtcreator+cmake compiler settings
我的理想工作,码农的绝对自由支配才是最重要的——未来创业的追求
与多家机构战略合作,背后彰显PlatoFarm元宇宙龙头的实力
[C language series] - a recursive topic
Elastic box flex
How to survive in the bear market of encryption market?
Sports health is deeply rooted in the hearts of the people, and move protocol leads quality life
[C language series] - constants and variables that confuse students
What is nmap and how to use it
Fantom (FTM) 在 FOMC会议之前飙升 45%