Android中使用AlarmManager设置闹钟

摘要:
要创建新的MainActivity,请添加时间选择器和按钮˂?
场景

设置闹钟

Android中使用AlarmManager设置闹钟第1张

闹钟提醒

Android中使用AlarmManager设置闹钟第2张

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

新建一个MainActivity,在其布局文件中添加一个时间选择器和一个Button

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="设置闹钟" />

</RelativeLayout>

然后在MainActivity中,将时间选择器的时分秒设置给日历对象,获取AlarmManager对象,然后设置闹钟,并提醒。

在设置闹钟的

 alarm.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pendingIntent);

其中AlarmManager.RTC_WAKEUP有如下几种类型

Android中使用AlarmManager设置闹钟第3张

然后后面的pendingIntent是封装了上面显示闹钟的Intent,显示闹钟的intent中跳转显示的页面AlarmActivity中

package com.badao.alarmmanager;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class AlarmActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AlertDialog alert = new AlertDialog.Builder(this).create();
        alert.setIcon(R.drawable.bg02);      //设置对话框的图标
        alert.setTitle("公众号:");       //设置对话框的标题
        alert.setMessage("霸道的程序猿");   //设置要显示的内容
        //添加确定按钮
        alert.setButton(DialogInterface.BUTTON_POSITIVE,"确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {}
        });
        alert.show();           // 显示对话框
    }
}

免责声明:文章转载自《Android中使用AlarmManager设置闹钟》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇GLIBC_2.18 not found 之类的问题解决办法Redhat安装 增强功能 virtualBox下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JavaScript OOP多态、封装、继承

封装   封装就是把抽象出来的数据和对数据的操作封装在一起,数据被保护在内部, 程序的其它部分只有通过被授权的操作(成员方法),才能对数据进行操作。 JS封装只有两种状态,一种是公开的,一种是私有的。 <script type="text/javascript"> function Person(name, agei, sal)...

PHP 自定义 alert 跳转方法

/** * 跳转方法 * @param $msg * @param null $path * @param null $parent */ public function alert($msg,$path=NULL,$parent=NULL){ if($parent ===...

人脸识别1:1对比 (二)

本项目采用Face++第三方接口,项目实现了两张图片的人脸识别和对比,得到相似度等信息 项目步骤如下: 一、所需权限 <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permissio...

android shape实现阴影或模糊边效果

1.实现阴影或模糊边效果方式: 2.通过shape来实现,具体是通过layer-list 多层叠放的方式实现的 1 <?xml version="1.0" encoding="utf-8"?> 2 <layer-list xmlns:android="http://schemas.android.com/apk/res/andro...

Android 实现动态匹配输入的内容 AutoCompleteTextView和MultiAutoCompleteTextView

AutoCompleteTextView1.功能:动态匹配输入的内容,如百度搜索引擎当输入文本时可以根据内容显示匹配的热门信息。2.独特属性:android:completionThreshold 设置输入多少字符时自动匹配 使用AutoCOmpleteTextView实现自动匹配输入的内容private AutoCompleteTextView acTe...

bootstrap基础(六)

其他内置组件 一、缩略图 使用方法: 通过“thumbnail”样式配合bootstrap的网格系统来实现。 <div class="container"> <div class="row"> <div class="col-xs-6 col-md-3"> <a hr...