zoukankan      html  css  js  c++  java
  • Android中使用AlarmManager设置闹钟

    场景

    设置闹钟

    闹钟提醒

    注:

    博客:
    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有如下几种类型

    然后后面的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 Studio:layout-sw600dp文件夹中创建activity_main.xml
    Android Studio提示忽略大小写
    学习进度条-3
    二维数组
    人月神话阅读笔记01
    作业-数组
    学习进度条-2
    周四测试
    家庭家长本-微信小程序
    第一周开课博客园
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/12178301.html
Copyright © 2011-2022 走看看