場景
設定鬧鐘

鬧鐘提醒

注:
博客:
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(); // 顯示對話框 } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/39321.html
標籤:Android
上一篇:11.Android-Xml讀寫
