运行效果:
activity_main.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <TimePicker 10 android:id="@+id/time" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content"/> //时间拾取器 13 14 <Button 15 android:id="@+id/set" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:layout_below="@+id/time" 19 android:layout_centerHorizontal="true" 20 android:text="set" 21 /> //设置闹钟按钮 22 23 </RelativeLayout>
MainActivity:
1 package com.mingrisoft.alarm; 2 3 import android.app.AlarmManager; 4 import android.app.PendingIntent; 5 import android.content.Context; 6 import android.content.Intent; 7 import android.support.v7.app.AppCompatActivity; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.widget.Button; 11 import android.widget.TimePicker; 12 import android.widget.Toast; 13 14 import java.util.Calendar; 15 16 public class MainActivity extends AppCompatActivity { 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 23 final TimePicker timePicker = findViewById(R.id.time); //时间拾取器 24 final Calendar calendar; //日历对象 25 26 calendar = Calendar.getInstance(); //获取日历对象 27 timePicker.setIs24HourView(true); //设置使用24小时制 28 29 Button button = findViewById(R.id.set); //获取“设置闹钟”按钮 30 //为按钮添加时间监听器 31 button.setOnClickListener(new View.OnClickListener() { 32 @Override 33 public void onClick(View v) { 34 //创建Intent对象 35 Intent intent = new Intent(MainActivity.this, AlarmActivity.class); 36 //获取显示闹钟的PendingIntent对象 37 PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0 , intent, 0); 38 //获取AlarmManager对象 39 AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 40 //设置闹钟的小时数 41 calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour()); 42 //设置闹钟的分钟数 43 calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute()); 44 //设置闹钟的秒数 45 calendar.set(Calendar.SECOND, 0); 46 //设置一个闹钟 47 alarmManager.set(alarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 48 //显示一个消息提示 49 Toast.makeText(MainActivity.this, "闹钟设置成功", Toast.LENGTH_SHORT).show(); 50 } 51 }); 52 } 53 }
创建一个AlarmActivity,用于显示闹钟提示的内容。在该Activity中重写onCreate()方法,在该方法中创建并显示一个带“确定”按钮的对话框,显示闹钟的提示内容:
1 package com.mingrisoft.alarm; 2 3 import android.content.DialogInterface; 4 import android.os.Bundle; 5 import android.support.annotation.Nullable; 6 import android.support.v7.app.AlertDialog; 7 import android.support.v7.app.AppCompatActivity; 8 9 public class AlarmActivity extends AppCompatActivity { 10 @Override 11 protected void onCreate(@Nullable Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 14 AlertDialog alert = new AlertDialog.Builder(this).create(); 15 alert.setIcon(R.drawable.alarm); //设置对话框的图标 16 alert.setTitle("传递正能量"); //设置对话框的标题 17 alert.setMessage("hemeiwolong"); //设置要要显示的内容 18 19 alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { 20 @Override 21 public void onClick(DialogInterface dialog, int which) { 22 23 } 24 }); 25 alert.show(); //显示对话框 26 } 27 }
AndroidManifest.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.mingrisoft.alarm"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:roundIcon="@mipmap/ic_launcher_round" 10 android:supportsRtl="true" 11 android:theme="@style/AppTheme"> 12 <activity android:name=".MainActivity"> 13 <intent-filter> 14 <action android:name="android.intent.action.MAIN" /> 15 16 <category android:name="android.intent.category.LAUNCHER" /> 17 </intent-filter> 18 </activity> 19 20 <activity 21 android:name=".AlarmActivity" 22 android:label="闹钟"> 23 </activity> 24 </application> 25 26 </manifest>
原文地址:https://www.cnblogs.com/hemeiwolong/p/12601729.html
时间: 2024-10-11 09:24:51