android 设置闹钟及通知示例
程序员文章站
2023-12-17 23:08:52
简单说一下这次demo内容,首先做一个设置一次性闹钟,先得到alarmmanager,打开一个时间对话框,在里面设置闹钟的时间,时间一到发送广播,然后广播接受者接到跳转到新...
简单说一下这次demo内容,首先做一个设置一次性闹钟,先得到alarmmanager,打开一个时间对话框,在里面设置闹钟的时间,时间一到发送广播,然后广播接受者接到跳转到新的activity播放音乐。接着是一个反复闹钟,最后是一个简单的通知,具体代码如下:
import android.app.alarmmanager; import android.app.notification; import android.app.notificationmanager; import android.app.pendingintent; import android.app.timepickerdialog; import android.content.context; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.app.notificationcompat; import android.view.view; import android.widget.timepicker; import java.util.calendar; public class mainactivity extends appcompatactivity { private alarmmanager alarmmanager; private pendingintent pendingintent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //获取闹钟管理者 alarmmanager = (alarmmanager) getsystemservice(context.alarm_service); } public void setalarm(view view){ //获取当前系统的时间 calendar calendar=calendar.getinstance(); int hour=calendar.get(calendar.hour_of_day); int minute=calendar.get(calendar.minute); //弹出时间对话框 timepickerdialog timepickerdialog=new timepickerdialog(this, new timepickerdialog.ontimesetlistener() { @override public void ontimeset(timepicker timepicker, int i, int i1) { calendar c=calendar.getinstance(); c.set(calendar.hour_of_day,i); c.set(calendar.minute,i1); intent intent=new intent(); intent.setaction("com.zking.g150820_android28_alarm_notification.ring"); //将来时态的跳转 ang eng ing ong pendingintent pendingintent=pendingintent.getbroadcast(mainactivity.this,0x101,intent,0); //设置闹钟 alarmmanager.set(alarmmanager.rtc_wakeup,c.gettimeinmillis(),pendingintent); //时间一到,发送广播(闹钟响了) //广播接受者中(跳转activity) // 跳转activity,在这个activity中播放音乐 } },hour,minute,true); timepickerdialog.show(); } public void setalarmcycle(view view){ //获取当前系统的时间 calendar calendar=calendar.getinstance(); int hour=calendar.get(calendar.hour_of_day); int minute=calendar.get(calendar.minute); //弹出时间对话框 timepickerdialog timepickerdialog=new timepickerdialog(this, new timepickerdialog.ontimesetlistener() { @override public void ontimeset(timepicker timepicker, int i, int i1) { calendar c=calendar.getinstance(); c.set(calendar.hour_of_day,i); c.set(calendar.minute,i1); intent intent=new intent(); intent.setaction("com.zking.g150820_android28_alarm_notification.ring"); //将来时态的跳转 ang eng ing ong pendingintent = pendingintent.getbroadcast(mainactivity.this,0x101,intent,0); //设置闹钟 // alarmmanager.set(alarmmanager.rtc_wakeup,c.gettimeinmillis(),pendingintent); //设置周期闹钟 alarmmanager.setrepeating(alarmmanager.rtc_wakeup,c.gettimeinmillis(),5000, pendingintent); //时间一到,发送广播(闹钟响了) //广播接受者中(跳转activity) // 跳转activity,在这个activity中播放音乐 } },hour,minute,true); timepickerdialog.show(); } public void cancelcycle(view view){ alarmmanager.cancel(pendingintent); } public void sendnotification(view view){ //实例化通知管理器 notificationmanager notificationmanager= (notificationmanager) getsystemservice(context.notification_service); //实例化通知 notificationcompat.builder builder=new notificationcompat.builder(this); builder.setcontenttitle("今日大事件"); builder.setcontenttext("骑士击败勇士夺冠"); builder.setdefaults(notificationcompat.default_all); builder.setautocancel(true); builder.setsmallicon(android.r.drawable.ic_media_play); builder.setcontentintent(pendingintent.getactivity(this,0x102,new intent(this,ringactivity.class),0)); notification notification=builder.build(); //发送通知 notificationmanager.notify(0x101,notification); } }
布局文件(xml):
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="ghq.zking.com.ghq_android_28.mainactivity"> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置闹钟(一次)" android:onclick="setalarm" /> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置闹钟(周期)" android:onclick="setalarmcycle" /> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消周期" android:onclick="cancelcycle" /> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送通知" android:onclick="sendnotification" /> </linearlayout>
广播接受者代码:
public class ringreceived extends broadcastreceiver { @override public void onreceive(context context, intent intent) { if ("ghq.zking.com.ghq_android_28_alarm_notification.ring".equals(intent.getaction())){ //跳转到activity intent intent1=new intent(context,ringactivity.class); //给intent设置标志位 intent1.setflags(intent.flag_activity_new_task); context.startactivity(intent1); } } }
闹钟播放activity代码:
public class ringactivity extends appcompatactivity { private mediaplayer mediaplayer; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_ring); mediaplayer = mediaplayer.create(this, r.raw.love); mediaplayer.start(); } public void stop(view view){ mediaplayer.stop(); finish(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
iOS UILabel 设置内容的间距及高度的计算示例
-
Android编程之PopupWindow隐藏及显示方法示例(showAtLocation,showAsDropDown)
-
Android编程使用AlarmManager设置闹钟的方法
-
Android编程实现设置按钮背景透明与半透明及图片背景透明的方法
-
Android编程之Application设置全局变量及传值用法实例分析
-
Android ListView 滚动条的设置详解及实例代码
-
Android编程实现获取系统内存、CPU使用率及状态栏高度的方法示例
-
android 跳转到应用通知设置界面的示例
-
Android实现调用系统图库与相机设置头像并保存在本地及服务器
-
Android ListView实现单选及多选等功能示例