您当前的位置:首页 > 毕业设计

毕业设计- Android闹钟

时间:2022-03-02 07:34:12 阅读数:51,468人阅读
版权声明:转载请注明出处,谢谢!
—— 未来真正会发生的事,其实完全取决于你现在做的决定。

项目介绍

1. 实现简单闹钟和日历闹钟两种模式,简单闹钟可以设置标签、铃声、单次闹钟或周一到周末重复性闹钟,日历闹钟在此基础上增加可以选择某月某日;

2. 可以查看闹钟列表,新建、编辑或删除闹钟;

3. 闹钟到时间后会弹出提示页面并振动响铃,点击即可关闭闹钟;

项目截图

实现原理

布局文件activity_main.xml:

			<?xml version="1.0" encoding="utf-8"?>
			<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
				xmlns:app="http://schemas.android.com/apk/res-auto"
				android:background="@color/colorPrimary"
				android:layout_width="match_parent"
				android:layout_height="match_parent"
				android:gravity="center">

				<FrameLayout
					android:id="@+id/main_activity_container"
					android:layout_width="match_parent"
					android:layout_height="match_parent"
					app:layout_constraintBottom_toTopOf="parent"
					app:layout_constraintEnd_toEndOf="parent"
					app:layout_constraintHorizontal_bias="0.0"
					app:layout_constraintStart_toStartOf="parent"
					app:layout_constraintVertical_bias="1.0">
				</FrameLayout>

				<com.google.android.material.bottomnavigation.BottomNavigationView
					android:id="@+id/bottomNavigationBar"
					android:layout_width="0dp"
					android:layout_height="wrap_content"
					android:layout_gravity="bottom"
					android:background="@color/colorPrimary"
					app:itemBackground="@color/colorPrimary"
					app:itemIconTint="@color/navigation_bar_selector"
					app:itemTextColor="@color/navigation_bar_selector"
					app:labelVisibilityMode="selected"
					app:layout_constraintBottom_toBottomOf="parent"
					app:layout_constraintEnd_toEndOf="parent"
					app:layout_constraintStart_toStartOf="parent"
					app:menu="@menu/bottom_navigation_menu" />

				<LinearLayout
					android:id="@+id/addAlarmBtn"
					android:layout_width="@dimen/fabWidth"
					android:layout_height="@dimen/fabHeight"
					android:paddingStart="@dimen/marginItemsInSectionSmall"
					android:paddingEnd="@dimen/marginItemsInSectionSmall"
					android:layout_marginBottom="@dimen/marginItemsInSectionLargest"
					android:layout_marginTop="@dimen/marginItemsInSectionSmall"
					android:background="@drawable/button_fab_extended_drawable"
					app:layout_constraintBottom_toBottomOf="parent"
					app:layout_constraintEnd_toEndOf="parent"
					app:layout_constraintStart_toStartOf="parent"
					android:gravity="center"
					android:elevation="1dp">
					<View
						android:layout_width="16dp"
						android:layout_height="16dp"
						android:background="@drawable/add"/>
					<Space
						android:layout_width="10dp"
						android:layout_height="match_parent" />
					<TextView
						android:layout_width="wrap_content"
						android:layout_height="wrap_content"
						android:text="@string/wake_up_at_set_hour_button"
						android:textColor="@android:color/white"
						android:fontFamily="@font/roboto"
						android:textSize="@dimen/textBody2"/>
				</LinearLayout>
			</androidx.constraintlayout.widget.ConstraintLayout>
	  
	       /**
			 * 闹钟接口实现类
			 */
			public class AlarmController {

				private Context context;
				private AlarmManager alarmManager;

				public AlarmController(Context context) {
					this.context = context;
					alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
				}

				/**
				 * 设置闹钟
				 * @param alarm Data object
				 */
				@TargetApi(19)
				public void setAlarm(Alarm alarm) {
					PendingIntent setAlarmPendingIntent
							= getAlarmPendingIntent(alarm, PendingIntent.FLAG_UPDATE_CURRENT);
					long executionDateMs = DateTime.parse(alarm.getTime()).getMillis();

					if(executionDateMs < DateTime.now().getMillis()) {
						Log.i("hel->", "Not alarm immediately.");
						return;
					}

					if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
						alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, executionDateMs,
								setAlarmPendingIntent);
					} else {
						alarmManager.setExact(AlarmManager.RTC_WAKEUP, executionDateMs, setAlarmPendingIntent);
					}
				}

				/**
				 * 取消闹钟
				 * @param alarm The exist alarm
				 */
				public void cancelAlarm(Alarm alarm) {
					PendingIntent cancelServicePendingIntent = getAlarmPendingIntent(alarm, 0);
					alarmManager.cancel(cancelServicePendingIntent);
				}

				/**
				 * 停止闹钟
				 */
				public void dismissCurrentlyPlayingAlarm() {
					context.stopService(new Intent(context, AlarmService.class));
				}

				private PendingIntent getAlarmPendingIntent(Alarm alarm, int flag) {
					String alarmId = alarm.getId();
					String ringtone = alarm.getRingtone();
					Intent alarmIntent = new Intent(context, AlarmReceiver.class);
					alarmIntent.putExtra(Const.KEYS.ALARM_ID, alarmId);
					if(!TextUtils.isEmpty(ringtone)) {
						alarmIntent.putExtra(Const.KEYS.RINGTONE_ID, ringtone);
					}

					int serviceId = getMostSignificantBits(alarmId);
					return PendingIntent.getBroadcast(context, serviceId, alarmIntent, flag);
				}

				private int getMostSignificantBits(String id) {
					return (int) Math.abs(UUID.fromString(id).getMostSignificantBits());
				}
			}
	  

购买套餐

权益 套餐A 套餐B
完整代码
远程调试 不支持
代码答疑 不支持
价 格 ¥150 ¥300

------转载请注明出处,感谢您对原创作者的支持------

有偿提供技术支持、Bug修复、项目外包、毕业设计、大小作业

Android学习小站

Q Q:1095817610

微信:jx-helu

邮箱:1095817610@qq.com

添加请备注"Android学习小站"