当前位置:网站首页>“山东大学移动互联网开发技术教学网站建设”项目实训日志四
“山东大学移动互联网开发技术教学网站建设”项目实训日志四
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();
}
}
}
后期工作规划:
- 下周开始教学网站的建设
边栏推荐
- Wechat applet - screen height
- 使用Qss设置窗体样式
- The completely decentralized programming mode does not need servers or IP, just like a aimless network extending everywhere
- 麦当娜“Hellbent”购买130万美元的nft无聊猿,现在被认为太贵了
- 365 day challenge leetcode1000 question - day 036 binary tree pruning + subarray and sorted interval sum + delete the shortest subarray to order the remaining arrays
- Summary of knowledge points related to forms and forms
- How to survive in the bear market of encryption market?
- MOVE PROTOCOL全球健康宣言,将健康运动进行到底
- Hcia-r & s self use notes (26) PPP
- 极致通缩和永动机模型,将推动 PlatoFarm 爆发
猜你喜欢

365 day challenge leetcode1000 question - day 036 binary tree pruning + subarray and sorted interval sum + delete the shortest subarray to order the remaining arrays

如何 Pr 一个开源composer项目

“山东大学移动互联网开发技术教学网站建设”项目实训日志六

深度学习的趣味app简单优化(适合新手)

Qframe class learning notes

The LAAS protocol of defi 2.0 is the key to revitalizing the development of defi track

如何在加密市场熊市中生存?

Crypto巨头们ALL IN元宇宙,PlatoFarm或能突围

IDEA使用JDBC连接MySQL数据库个人详细教程

极致通缩和永动机模型,将推动 PlatoFarm 爆发
随机推荐
Fvuln-自动化web漏洞检测工具
Installation steps and environment configuration of vs Code
Thinkphp6管道模式Pipeline使用
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
[C language series] - string + partial escape character explanation + annotation tips
QT layout management -- Part stretch principle and sizepolicy
Strategic cooperation with many institutions shows the strength of the leading company of platofarm yuancosmos
Hcia-r & s self use notes (24) ACL
Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
打印出1-100之间的所有质数
如何在加密市场熊市中生存?
量化开发必掌握的30个知识点【什么是Level-2数据】
Similarities and differences between REM and PX and EM
The completely decentralized programming mode does not need servers or IP, just like a aimless network extending everywhere
Merge the same items in the same column in table
Differences between href and SRC
Win10 搭建MSYS2环境
How does the MD editor of CSDN input superscripts and subscripts? The input method of formula and non formula is different
我的理想工作,码农的绝对自由支配才是最重要的——未来创业的追求
Laravel服务容器(继承与事件)