設置鬧鐘android
鬧鐘提醒編程
注:app
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公衆號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。ide
新建一個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對象,而後設置鬧鐘,並提醒。ui
在設置鬧鐘的this
alarm.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pendingIntent);
其中AlarmManager.RTC_WAKEUP有以下幾種類型spa
而後後面的pendingIntent是封裝了上面顯示鬧鐘的Intent,顯示鬧鐘的intent中跳轉顯示的頁面AlarmActivity中.net
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(); // 顯示對話框 } }