android 设置闹钟及通知示例
更新时间:2017年02月16日 08:29:43 作者:夜的千章
本篇文章主要介绍了android 设置闹钟及通知示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
简单说一下这次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(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Android开发实现模仿微信小窗口功能【Dialog对话框风格窗口】
这篇文章主要介绍了Android开发实现模仿微信小窗口功能,结合实例形式分析了Android实现微信风格Dialog对话框窗口相关功能与布局操作技巧,需要的朋友可以参考下2019-03-03android studio 新手入门教程(三)Github( ignore忽略规则)的使用教程图解
这篇文章主要介绍了android studio 新手入门教程(三)Github( ignore忽略规则)的使用教程图解,需要的朋友可以参考下2017-12-12Android Studio 3.3.2 正式版的安装教程图解
这篇文章主要介绍了Android Studio 3.3.2 正式版的安装教程图解,本文分步骤通过图文并茂的形式给大家介绍的非常详细,需要的朋友可以参考下2020-02-02
最新评论