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

Android编程使用AlarmManager设置闹钟的方法

程序员文章站 2023-12-16 20:58:22
本文实例讲述了android编程使用alarmmanager设置闹钟的方法。分享给大家供大家参考,具体如下: package com.aina.android;...

本文实例讲述了android编程使用alarmmanager设置闹钟的方法。分享给大家供大家参考,具体如下:

package com.aina.android;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.widget.toast;
/**
 * com.aina.android
 * pro_alarmmanager
 * @author aina.huang e-mail: 674023920@qq.com
 * @version 创建时间:2010 jul 8, 2010 3:03:19 pm
 * 类说明
 */
public class alamrreceiver extends broadcastreceiver {
  @override
  public void onreceive(context context, intent intent) {
    // todo auto-generated method stub
    toast.maketext(context, "闹钟时间到", toast.length_long).show();
  }
}

package com.aina.android;
import java.util.calendar;
import android.app.activity;
import android.app.alarmmanager;
import android.app.pendingintent;
import android.app.timepickerdialog;
import android.content.intent;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.textview;
import android.widget.timepicker;
public class test extends activity {
  /** called when the activity is first created. */
  private textview tv = null;
  private button btn_set = null;
  private button btn_cel = null;
  private calendar c = null;
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    tv = (textview) this.findviewbyid(r.id.textview);
    btn_set = (button) this.findviewbyid(r.id.button01);
    btn_cel = (button) this.findviewbyid(r.id.button02);
    c = calendar.getinstance();
    btn_set.setonclicklistener(new button.onclicklistener(){
      public void onclick(view v) {
        // todo auto-generated method stub
        c.settimeinmillis(system.currenttimemillis());
        int hour = c.get(calendar.hour_of_day);
        int minute = c.get(calendar.minute);
        new timepickerdialog(test.this,new timepickerdialog.ontimesetlistener(){
          public void ontimeset(timepicker view, int hourofday,
              int minute) {
            // todo auto-generated method stub
            c.settimeinmillis(system.currenttimemillis());
            c.set(calendar.hour_of_day, hourofday);
            c.set(calendar.minute, minute);
            c.set(calendar.second, 0);
            c.set(calendar.millisecond, 0);
            intent intent = new intent(test.this,alamrreceiver.class);
            pendingintent pi = pendingintent.getbroadcast(test.this, 0, intent, 0);
            alarmmanager am = (alarmmanager) getsystemservice(activity.alarm_service);
            am.set(alarmmanager.rtc_wakeup, c.gettimeinmillis(), pi);//设置闹钟
            am.setrepeating(alarmmanager.rtc_wakeup, c.gettimeinmillis(), (10*1000), pi);//重复设置
            tv.settext("设置的闹钟时间为:"+hourofday+":"+minute);
          }
        },hour,minute,true).show();
      }
    });
    btn_cel.setonclicklistener(new button.onclicklistener(){
      public void onclick(view v) {
        // todo auto-generated method stub
        intent intent = new intent(test.this,alamrreceiver.class);
        pendingintent pi = pendingintent.getbroadcast(test.this, 0, intent, 0);
        alarmmanager am = (alarmmanager) getsystemservice(activity.alarm_service);
        am.cancel(pi);
        tv.settext("闹钟取消");
      }
    });
  }
}

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <textview android:layout_width="fill_parent"
    android:id="@+id/textview"
    android:layout_height="wrap_content" android:text="@string/hello" />
  <button android:text="设置闹钟" android:id="@+id/button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
  </button>
  <button android:text="取消闹钟" android:id="@+id/button02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
  </button>
</linearlayout>

androidmanifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.aina.android"
   android:versioncode="1"
   android:versionname="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".test"
         android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.main" />
        <category android:name="android.intent.category.launcher" />
      </intent-filter>
    </activity>
    <receiver android:name=".alamrreceiver" android:process=":remote"></receiver>
  </application>
</manifest>

ps:关于androidmanifest.xml文件相关属性功能可参考本站在线工具:

android manifest功能与权限描述大全:
http://tools.jb51.net/table/androidmanifest

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: