欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现简易闹钟功能

程序员文章站 2023-11-25 16:40:58
本文实例为大家分享了android通过广播来实现闹钟的具体代码,供大家参考,具体内容如下 1.创建广播接收repeatingalarm.java impor...

本文实例为大家分享了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对象。

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