随笔 - 79  文章 - 0  评论 - 0  阅读 - 3202

第四周星期二每日总结

      本日继续完善每日打卡app,今日完善制作了打卡app的设置闹钟提醒的功能,能够在用户

登陆后选择设置指定时间打卡,时间到后系统发出提醒,页面可提供用户前往打卡和继续设置闹钟的服务。

代码如下:

activity_tip.xml    //设置闹钟页面

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context=".tip">
 9 
10     <TimePicker
11         android:id="@+id/time"
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         tools:ignore="InvalidId" />
15 
16     <Button
17         android:id="@+id/button"
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content"
20         android:text="设置闹钟"
21         />
22 </LinearLayout>
复制代码

android_time.xml //闹钟到时后提醒页面

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".time">
 8 
 9     <ImageView
10         android:id="@+id/imageView"
11         android:layout_width="0dp"
12         android:layout_height="0dp"
13         android:text="打卡时间到了"
14         app:layout_constraintBottom_toBottomOf="parent"
15         app:layout_constraintEnd_toEndOf="parent"
16         app:layout_constraintStart_toStartOf="parent"
17         app:layout_constraintTop_toTopOf="parent" />
18 
19     <Button
20         android:id="@+id/button"
21         android:layout_width="0dp"
22         android:layout_height="wrap_content"
23         android:layout_marginBottom="340dp"
24         android:text="前往打卡"
25         app:layout_constraintBottom_toBottomOf="parent"
26         app:layout_constraintEnd_toEndOf="parent"
27         app:layout_constraintHorizontal_bias="0.0"
28         app:layout_constraintStart_toStartOf="parent" />
29 </androidx.constraintlayout.widget.ConstraintLayout>
复制代码

tip.java

复制代码
 1 package com.example.null001;
 2 import androidx.appcompat.app.AppCompatActivity;
 3 import android.app.AlarmManager;
 4 import android.app.PendingIntent;
 5 import android.content.Context;
 6 import android.content.Intent;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.TimePicker;
11 import java.util.Calendar;
12 
13 public class tip extends AppCompatActivity {
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_tip);
19         final TimePicker timePicker=findViewById(R.id.time);  //获取时间拾取组件
20         Button button=findViewById(R.id.button);
21         button.setOnClickListener(new View.OnClickListener() {   //给'设置闹钟'按钮设置监听
22             @Override
23             public void onClick(View v) {
24                 Intent intent=new Intent(tip.this,time.class);
25                 PendingIntent pend=PendingIntent.getActivity(tip.this,0,intent,0); //显示闹钟,alarmActivity
26                 AlarmManager alarm= (AlarmManager) getSystemService(Context.ALARM_SERVICE);       // 通过Context.ALARM_SERVICE获取AlarmManager对象
27                 Calendar calendar =Calendar.getInstance();                     //获取日历对象
28                 calendar.set(Calendar.HOUR_OF_DAY,timePicker.getHour());       //利用时间拾取组件timePicker得到要设定的时间
29                 calendar.set(Calendar.MINUTE,timePicker.getMinute());
30                 calendar.set(Calendar.SECOND,0);
31                 alarm.set(AlarmManager.RTC,calendar.getTimeInMillis(),pend);     //设定闹钟
32 
33                 //AlarmManager.ELAPSED_REALTIME 在指定延迟后提醒
34                 //AlarmManager.ELAPSED_REALTIME_WAKEUP 在指定延迟后提醒,并唤醒系统
35                 //AlarmManager.RTC     在指定时间提醒
36                 //AlarmManager.RTC_WAKEUP    在指定时间提醒并唤醒系统
37             }
38         });
39     }
40 }
复制代码

time.java

复制代码
 1 package com.example.null001;
 2 
 3 import android.app.AlarmManager;
 4 import android.app.PendingIntent;
 5 import android.content.Context;
 6 import android.content.Intent;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.TimePicker;
11 import android.widget.Toast;
12 
13 import androidx.appcompat.app.AppCompatActivity;
14 
15 import java.util.Calendar;
16 
17 public class time extends AppCompatActivity {
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_time);
21         Button button=findViewById(R.id.button);
22         button.setOnClickListener(new View.OnClickListener() {   //给'设置闹钟'按钮设置监听
23             @Override
24             public void onClick(View v) {
25                 Intent intent=new Intent(time.this,index.class);
26                 startActivity(intent);
27                 Toast.makeText(time.this,"前往打卡!",Toast.LENGTH_SHORT).show();
28             }
29         });
30     }
31 }
复制代码

 

posted on   樱华旧梦  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示