Setting alarm clock with AlarmManager in Android

scene

set alarm

 

 

Alarm reminder

 

 

Note:

Blog:
https://blog.csdn.net/badao_liumang_qizhi
Pay attention to the public address
Domineering procedural ape
Get programming related ebooks, tutorials and free downloads.

Realization

Create a new MainActivity, add a time selector and a Button to its layout file

<?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="set alarm" />

</RelativeLayout>

 

Then in MainActivity, set the hour, minute and second of time selector to calendar object, get AlarmManager object, then set alarm clock and remind.

Setting the alarm

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

Among them, alarmmanager.rtc ﹣ wakeup has the following types

 

 

Then the pendingIntent at the back is the one that encapsulates the alarm clock displayed above. In the Intent that displays the alarm clock, jump to the page that displays 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);      //Set icon for dialog box
        alert.setTitle("Public address:");       //Set the title of the dialog box
        alert.setMessage("Domineering procedural ape");   //Set what to display
        //Add OK button
        alert.setButton(DialogInterface.BUTTON_POSITIVE,"Determine", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {}
        });
        alert.show();           // display a dialog box
    }
}

Tags: Android Programming xml encoding

Posted on Fri, 10 Jan 2020 10:16:26 -0500 by justinchrono