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

Android实现闹钟功能小Dome

程序员文章站 2023-12-15 09:01:04
闹钟闹不醒的可以自己去调整下,这个最是最基本的 mainactivity public class mainactivity extends appcompat...

闹钟闹不醒的可以自己去调整下,这个最是最基本的

mainactivity

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 setalarmone(view view) {
    //获取当前系统时间
    calendar calendar = calendar.getinstance();
    int huor = calendar.get(calendar.hour_of_day);
    int minute = calendar.get(calendar.minute);


    //1 弹出时间对话框
    timepickerdialog timepickerdialog = new timepickerdialog(this, new timepickerdialog.ontimesetlistener() {

      @override
      public void ontimeset(timepicker view, int i, int i1) {
        calendar c = calendar.getinstance();
        c.set(calendar.hour_of_day, i);
        c.set(calendar.minute, i1);
        //2确定好选择的时间
        //3设置闹钟
        //4当时间一到,将执行的响应
        intent intent=new intent();
        intent.setaction("com.zking.risk_android_alarn_notification.ring");
        pendingintent = pendingintent.getbroadcast(mainactivity.this,0x101,intent,0);
        alarmmanager.set(alarmmanager.rtc_wakeup,c.gettimeinmillis(), pendingintent);
      }
    }, huor, 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);

    //01.弹出时间对话框
    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);
        //02.确定好选择的时间
        //03.设置闹钟

        //04.当之间一到,将执行的响应
        intent intent=new intent();
        intent.setaction("com.zking.risk_android_alarn_notification.ring");
        pendingintent = pendingintent.getbroadcast(mainactivity.this,0x102,intent,0);
        alarmmanager.setrepeating(alarmmanager.rtc_wakeup,c.gettimeinmillis(),5000, pendingintent);
      }
    },hour,minute,true);
    timepickerdialog.show();

  }

  public void cancelalarmcycle(view view){
    alarmmanager.cancel(pendingintent);
  }


}

ringactivity

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.rooster);
    mediaplayer.start();
  }

  public void close(view view) {
    mediaplayer.stop();
    finish();
  }
}

ringreceived

public class ringreceived extends broadcastreceiver{
  @override
  public void onreceive(context context, intent intent) {
    if("com.zking.risk_android_alarn_notification.ring".equals(intent.getaction())){
      log.i("test","收到广播了");

      //跳转到activity
      intent intent1=new intent(context,ringactivity.class);
      //给intent设置标志位flag
      intent1.setflags(intent.flag_activity_new_task);
      //intent的组成部分:componementname extra action data categlay(分组) flag
      context.startactivity(intent1);
    }
  }
}

activity_main

<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="com.zking.risk_android_alarn_notification.mainactivity">

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="设置闹钟(一次)"
    android:onclick="setalarmone"
    />
  <button
    android:layout_width="wrap_content"
    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="cancelalarmcycle"
  />

</linearlayout>

actiity_ring

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_ring"
  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="com.zking.risk_android_alarn_notification.ringactivity"
  android:weightsum="1">
  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="亲爱的,该起床了"
    android:textsize="30sp"
    />

  <timepicker
    android:layout_width="324dp"
    android:layout_height="378dp"
    android:id="@+id/timepicker"
    android:layout_weight="0.30" />

<button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="关闭"
  android:onclick="close"
  />


</linearlayout>

最后的清单配置

 <activity android:name=".ringactivity"></activity>

    <receiver android:name=".ringreceived">
      <intent-filter>
        <action android:name="com.zking.risk_android_alarn_notification.ring"></action>
      </intent-filter>

    </receiver>

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

上一篇:

下一篇: