Set an alarm using Android AlarmManager

Scenes

Set the alarm

 

Alarm reminder

 

Note:

Blog:
https://blog.csdn.net/badao_liumang_qizhi
public concern number of
programs overbearing ape
acquisition-related programming e-books, tutorials and push for free download.

achieve

Create a MainActivity, adding a selector and a time at which the layout file 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>

Then MainActivity, the minutes and seconds when the time selector to set the calendar object, get AlarmManager object, and then set the alarm and alert.

In setting the alarm

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

There are several types wherein AlarmManager.RTC_WAKEUP

 

The latter is then encapsulated pendingIntent shown above alarm Intent, alarm display intent in the jump in the displayed page 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();           // 显示对话框
    }
}

 

Released 1182 original articles · won praise 207 · views 680 000 +

Guess you like

Origin blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103931329