Android实现简易闹钟功能

所属分类: 软件编程 / Android 阅读数: 54
收藏 0 赞 0 分享

本文实例为大家分享了Android通过广播来实现闹钟的具体代码,供大家参考,具体内容如下

1.创建广播接收RepeatingAlarm.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class RepeatingAlarm extends BroadcastReceiver{

 @Override
 public void onReceive(Context context, Intent intent) {
  if (intent.getAction()!=null&&intent.getAction().equals("com.gcc.alarm")) {//自定义的action
   intent = new Intent(context,AlarmActivity.class);
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   context.startActivity(intent);
  }
 }
}

2.广播在Manifest.xml中配置:

<receiver 
 android:name=".RepeatingAlarm"
 >
  <intent-filter > 
   <action android:name="com.gcc.alarm"/> 
   </intent-filter> 
</receiver>

3.通过代码设置一个闹钟

Intent intent = new Intent(this, RepeatingAlarm.class);
intent.setAction("com.gcc.alarm");
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
     am.set(AlarmManager.RTC,
       c.getTimeInMillis(), sender);//c为设置闹钟的时间的Calendar对象

4.通过代码取消一个闹钟:

/**
 * 取消闹钟
 */
private void cancleAlarm(){
 Intent intent = new Intent(AlarmActivity.this,RepeatingAlarm.class);
 intent.setAction("com.gcc.alarm");
 PendingIntent sender = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
 // And cancel the alarm.
 AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
  am.cancel(sender);//取消闹钟
 }

5.闹钟响是弹出的对化框并播放音乐用AlarmActivity.java类实现

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;

public class AlarmActivity extends Activity {

 MediaPlayer mp;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.aty_alarm);
  mp = new MediaPlayer();
  AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);
  try {
   mp.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
     file.getLength());
   mp.prepare();
   file.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  mp.setVolume(0.5f, 0.5f);
  mp.setLooping(true);
  mp.start();
  alarmOialog();
 }

 @Override
 protected void onResume() {
  super.onResume();
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  if (mp != null) {
   if (mp.isPlaying()) {
    mp.stop();
   }
   mp.release();
  }
 }

 public void alarmOialog() {
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setMessage("你有未处理的事件");
  builder.setPositiveButton("稍后提醒",
    new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialogInterface, int i) {
      alarm();
      finish();
     }
    });

  builder.setNegativeButton("停止", new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialogInterface, int i) {
    cancleAlarm();
    finish();// 关闭窗口
   }
  });
  builder.show().setCanceledOnTouchOutside(false);
  ;

 }

 /**
  * 取消闹钟
  */
 private void cancleAlarm() {
  // Create the same intent, and thus a matching IntentSender, for
  // the one that was scheduled.
  Intent intent = new Intent(AlarmActivity.this, RepeatingAlarm.class);
  intent.setAction("com.gcc.alarm");
  PendingIntent sender = PendingIntent.getBroadcast(AlarmActivity.this,
    0, intent, 0);

  // And cancel the alarm.
  AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  am.cancel(sender);
 }

 private void alarm() {
  // 获取系统的闹钟服务
  AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  // 触发闹钟的时间(毫秒)
  long triggerTime = System.currentTimeMillis() + 10000;
  Intent intent = new Intent(this, RepeatingAlarm.class);
  intent.setAction("com.gcc.alarm");
  PendingIntent op = PendingIntent.getBroadcast(this, 0, intent, 0);
  // 启动一次只会执行一次的闹钟
  am.set(AlarmManager.RTC, triggerTime, op);
  // 指定时间重复执行闹钟
  // am.setRepeating(AlarmManager.RTC,triggerTime,2000,op);
 }

}

6.注:

1.aty_alarm.xml为空布局,不需添加任何组件
2.使用MediaPlayer播放res/raw目录下音频文件的方法如下:

mp = new MediaPlayer();
  AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);
  try {
   mp.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
     file.getLength());

7.功能不是很完善,需要的可以修改使用,闹钟时间设定可通过上篇博文来获取Calendar对象。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android 自定义球型水波纹带圆弧进度效果(实例代码)

最近小编接到一个这样的需求,需要实现一个圆形水波纹,带进度,两层水波纹需要渐变显示,且外围有一个圆弧进度。今天小编给大家分享实例代码,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Flutter 实现下拉刷新上拉加载的示例代码

这篇文章主要介绍了Flutter 实现下拉刷新上拉加载的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Windows实现Flutter环境搭建及配置这一篇就够了

这篇文章主要介绍了Windows实现Flutter环境搭建及配置这一篇就够了,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android利用碎片fragment实现底部标题栏(Github模板开源)

Fragment可以作为Activity的组成部分,一个Activity可以有多个Fragment,这篇文章主要介绍了Android利用碎片fragment实现底部标题栏(Github模板开源),需要的朋友可以参考下
收藏 0 赞 0 分享

android studio 的下拉菜单Spinner使用详解

这篇文章主要介绍了android studio 的下拉菜单Spinner使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解析Android 8.1平台SystemUI 导航栏加载流程

这篇文章主要介绍了Android 8.1平台SystemUI 导航栏加载流程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信录音功能

这篇文章主要为大家详细介绍了Android仿微信录音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信键盘切换效果

这篇文章主要为大家详细介绍了Android仿微信键盘切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android超清晰6.0权限申请AndPermission

这篇文章主要介绍了Android超清晰6.0权限申请AndPermission,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信录制语音功能

这篇文章主要介绍了Android仿微信录制语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多