120闹钟AlarmManage

主屏幕有四个按键,设置一次闹钟,取消,设置重复闹钟,取消

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layout1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/white"
>
  <DigitalClock
    android:id="@+id/dClock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="40sp"
    android:textColor="@drawable/blue"
    android:layout_x="52dp"
    android:layout_y="3dp"
  >
  </DigitalClock>
  <TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_title1"
    android:textSize="20sp"
    android:textColor="@drawable/black"
    android:layout_x="9dp"
    android:layout_y="61dp"
  >
  </TextView>
  <Button
    android:id="@+id/mButton1"
    android:layout_width="wrap_content"
    android:layout_height="40px"
    android:text="@string/str_button1"
    android:textColor="@drawable/black"
    android:textSize="18sp"
    android:layout_x="250dp"
    android:layout_y="70dp"
  >
  </Button>
  <Button
    android:id="@+id/mButton2"
    android:layout_width="wrap_content"
    android:layout_height="40px"
    android:text="@string/str_button2"
    android:textColor="@drawable/black"
    android:textSize="18sp"
    android:layout_x="250dp"
    android:layout_y="120dp"
  >
  </Button>
  <TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_title2"
    android:textSize="20sp"
    android:textColor="@drawable/black"
    android:layout_x="11dp"
    android:layout_y="164dp"
  >
  </TextView>
  <Button
    android:id="@+id/mButton3"
    android:layout_width="wrap_content"
    android:layout_height="40px"
    android:text="@string/str_button1"
    android:textColor="@drawable/black"
    android:textSize="18sp"
    android:layout_x="250dp"
    android:layout_y="170dp"
  >
  </Button>
  <Button
    android:id="@+id/mButton4"
    android:layout_width="wrap_content"
    android:layout_height="40px"
    android:text="@string/str_button2"
    android:textColor="@drawable/black"
    android:textSize="18sp"
    android:layout_x="250dp"
    android:layout_y="220dp"
  >
  </Button>
  <TextView
    android:id="@+id/setTime1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_default"
    android:textColor="@drawable/red"
    android:textSize="16sp"
    android:layout_x="14dp"
    android:layout_y="109dp"
  >
  </TextView>
  <TextView
    android:id="@+id/setTime2"
    android:layout_width="170px"
    android:layout_height="wrap_content"
    android:text="@string/str_default"
    android:textColor="@drawable/red"
    android:textSize="16sp"
    android:layout_x="16dp"
    android:layout_y="215dp"
  >
  </TextView>
</AbsoluteLayout>

设置四个按钮的响应事件,设置和取消闹铃

public class example112 extends Activity
{
  /* 声明对象变量 */
  TextView setTime1;
  TextView setTime2;
  Button mButton1;
  Button mButton2;
  Button mButton3;
  Button mButton4;
  Calendar c=Calendar.getInstance();

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    /* 载入main.xml Layout */
    setContentView(R.layout.main);

    //设置只响一次的闹铃
    SetOneTimeAlarm();

    CancelOneTimeAlarm();
    SetRepeatAlarm();


    CancelRepeatAlarm();


  }

  private void SetOneTimeAlarm() {
    /* 以下为只响一次的闹钟的设置 */
    setTime1=(TextView) findViewById(R.id.setTime1);
    /* 只响一次的闹钟的设置Button */
    mButton1=(Button)findViewById(R.id.mButton1);
    mButton1.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        /* 取得点击按钮时的时间作为TimePickerDialog的默认值 */
        c.setTimeInMillis(System.currentTimeMillis());
        int mHour=c.get(Calendar.HOUR_OF_DAY);
        int mMinute=c.get(Calendar.MINUTE);

        /* 跳出TimePickerDialog来设置时间 */
        new TimePickerDialog(example112.this, getTimeSetListener(),mHour,mMinute,true).show();
      }

      private TimePickerDialog.OnTimeSetListener getTimeSetListener() {
        return new TimePickerDialog.OnTimeSetListener() {
          public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            //获取picktime设置的时间,保存到c
            getSetTime(hourOfDay, minute);
            //设置c时间执行闹铃,只执行一次
            SetAlarm();
            //更新设置后的闹铃信息显示
            UpdateAlarmTime(hourOfDay, minute);
          }
          private void getSetTime(int hourOfDay, int minute) {
             /* 取得TimePicker的时间,并保存到c,秒跟毫秒设为0 */
            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);
          }
          private void SetAlarm() {
             /* 指定闹钟设置时间到时要运行CallAlarm.class */
            Intent intent = new Intent(example112.this, example112_2.class);
            /* 创建PendingIntent
            * 执行闹铃时发送一条广播
            * */
            PendingIntent sender=PendingIntent.getBroadcast(example112.this,0, intent, 0);
            /* AlarmManager.RTC_WAKEUP设置服务在系统休眠时同样会运行
             * 以set()设置的PendingIntent只会运行一次
             * 闹钟会在c时间发送一条广播
             * */
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), sender);
          }
          private void UpdateAlarmTime(int hourOfDay, int minute) {
            /* 更新显示的设置闹钟时间 */
            String tmpS=format(hourOfDay)+":"+format(minute);
            setTime1.setText(tmpS);
            /* 以Toast提示设置已完成 */
            Toast.makeText(example112.this,"设置闹钟时间为"+tmpS,
                    Toast.LENGTH_SHORT)
                    .show();
          }
        };
      }
    });
  }


  private void CancelOneTimeAlarm() {
    /* 只响一次的闹钟的删除Button */
    mButton2=(Button) findViewById(R.id.mButton2);
    mButton2.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View v)
      {
        Intent intent = new Intent(example112.this, example112_2.class);
        PendingIntent sender=PendingIntent.getBroadcast(example112.this,0, intent, 0);
        /* 由AlarmManager中删除 */
        AlarmManager am;
        am =(AlarmManager)getSystemService(ALARM_SERVICE);
        am.cancel(sender);
        /* 以Toast提示已删除设置,并更新显示的闹钟时间 */
        Toast.makeText(example112.this,"闹钟时间解除", Toast.LENGTH_SHORT).show();
        setTime1.setText("目前无设置");
      }
    });
  }
private void SetRepeatAlarm() {
    /* 以下为重复响起的闹钟的设置 */
    setTime2=(TextView) findViewById(R.id.setTime2);
    /* create重复响起的闹钟的设置画面 */
    /* 引用timeset.xml为Layout */
    LayoutInflater factory = LayoutInflater.from(this);
    final View setView = factory.inflate(R.layout.timeset,null);
    final TimePicker tPicker=(TimePicker)setView.findViewById(R.id.tPicker);
    tPicker.setIs24HourView(true);

    /* create重复响起闹钟的设置Dialog */
    final AlertDialog di=new AlertDialog.Builder(example112.this)
          .setIcon(R.drawable.clock)
          .setTitle("设置")
          .setView(setView)
          .setPositiveButton("确定", getRepearAlarmListener(setView, tPicker))
          .setNegativeButton("取消",
            new DialogInterface.OnClickListener()
           {
             public void onClick(DialogInterface dialog, int which)
             {
             }
           }).create();

    /* 重复响起的闹钟的设置Button */
    mButton3=(Button) findViewById(R.id.mButton3);
    mButton3.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View v)
      {
        /* 取得点击按钮时的时间作为tPicker的默认值 */
        c.setTimeInMillis(System.currentTimeMillis());
        tPicker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
        tPicker.setCurrentMinute(c.get(Calendar.MINUTE));
        /* 跳出设置画面di */
        di.show();
      }
    });
  }
 private DialogInterface.OnClickListener getRepearAlarmListener(final View setView, final TimePicker tPicker) {
    return new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int which)
     {
       /* 取得设置的间隔秒数 */
       EditText ed=(EditText)setView.findViewById(R.id.mEdit);
       int times=Integer.parseInt(ed.getText().toString())
                  *1000;
       /* 取得设置的开始时间,秒及毫秒设为0 */
       c.setTimeInMillis(System.currentTimeMillis());
       c.set(Calendar.HOUR_OF_DAY,tPicker.getCurrentHour());
       c.set(Calendar.MINUTE,tPicker.getCurrentMinute());
       c.set(Calendar.SECOND,0);
       c.set(Calendar.MILLISECOND,0);

       /* 指定闹钟设置时间到时要运行CallAlarm.class */
       Intent intent = new Intent(example112.this,
                                  example112_2.class);
       PendingIntent sender = PendingIntent.getBroadcast(
                                example112.this,1, intent, 0);
       /* setRepeating()可让闹钟重复运行 */
       AlarmManager am;
       am = (AlarmManager)getSystemService(ALARM_SERVICE);
       am.setRepeating(AlarmManager.RTC_WAKEUP,
                 c.getTimeInMillis(),times,sender);
       /* 更新显示的设置闹钟时间 */
       String tmpS=format(tPicker.getCurrentHour())+":"+
                   format(tPicker.getCurrentMinute());
       setTime2.setText("设置闹钟时间为"+tmpS+
                        "开始,重复间隔为"+times/1000+"秒");
       /* 以Toast提示设置已完成 */
       Toast.makeText(example112.this,"设置闹钟时间为"+tmpS+
                      "开始,重复间隔为"+times/1000+"秒",
                      Toast.LENGTH_SHORT).show();
     }
   };
  }
  /* 日期时间显示两位数的方法 */
  private String format(int x)
  {
    String s=""+x;
    if(s.length()==1) s="0"+s;
    return s;
  }
}

按下设置时的设置界面

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
  android:id="@+id/layout2"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
>
  <TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_text1"
    android:textColor="@drawable/white"
    android:textSize="16sp"
    android:layout_x="99dp"
    android:layout_y="24dp"
  >
  </TextView>
  <TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_text2"
    android:textColor="@drawable/white"
   	android:textSize="16sp"
    android:layout_x="2dp"
    android:layout_y="277dp"
  >
  </TextView>

  <EditText
    android:id="@+id/mEdit"
    android:layout_width="52px"
    android:layout_height="40px"
    android:text="15"
    android:textSize="16sp"
    android:layout_x="85dp"
    android:layout_y="279dp"
  >
  </EditText>
  <TextView
    android:id="@+id/text3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_text3"
    android:textColor="@drawable/white"
    android:textSize="16sp"
    android:layout_x="160dp"
    android:layout_y="277dp"
  >
  </TextView>

  <TimePicker
    android:id="@+id/tPicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="44dp"
    android:layout_y="71dp"
  >
  </TimePicker>
</AbsoluteLayout>



private void CancelRepeatAlarm() {
    /* 重复响起的闹钟的删除Button */
    mButton4=(Button) findViewById(R.id.mButton4);
    mButton4.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View v)
      {
        Intent intent = new Intent(example112.this, example112_2.class);
        PendingIntent sender = PendingIntent.getBroadcast(example112.this,1, intent, 0);
        /* 由AlarmManager中删除 */
        AlarmManager am;
        am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.cancel(sender);
        /* 以Toast提示已删除设置,并更新显示的闹钟时间 */
        Toast.makeText(example112.this,"闹钟时间解除",
                Toast.LENGTH_SHORT).show();
        setTime2.setText("目前无设置");
      }
    });
  }

当闹铃时间到了时,会发送广播

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="1"
    android:versionName="1.0.0" package="irdc.example112">
  <application
      android:icon="@drawable/icon" android:label="@string/app_name">
    <!-- 注册receiver CallAlarm -->
    <receiver
        android:name="irdc.example112.example112_2" android:process=":remote" />
    <activity android:name=".example112" android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name="irdc.example112.example112_1" android:label="@string/app_name">
    </activity>
  </application>
</manifest>
接收广播后会启动Activity

package irdc.example112;

/* import相关class */
import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.os.Bundle;

/* 调用闹钟Alert的Receiver */
public class example112_2 extends BroadcastReceiver
{
  @Override
  public void onReceive(Context context, Intent intent)
  {
    /* create Intent,调用AlarmAlert.class */
    Intent i = new Intent(context, example112_1.class);
        
    Bundle bundleRet = new Bundle();
    bundleRet.putString("STR_CALLER", "");
    i.putExtras(bundleRet);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //接到闹铃后启动提醒Activity
    context.startActivity(i);
  }
}
package irdc.example112;

/* import相关class */
import irdc.example112.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

/* 实际跳出闹铃Dialog的Activity */
public class example112_1 extends Activity
{
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /* 跳出的闹铃警示  */
    new AlertDialog.Builder(example112_1.this)
        .setIcon(R.drawable.clock)
        .setTitle("闹钟响了!!")
        .setMessage("赶快起床吧!!!")
        .setPositiveButton("关掉他", getListener())
        .show();
  }

  private DialogInterface.OnClickListener getListener() {
    return new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
       /* 关闭Activity */
       example112_1.this.finish();
     }
   };
  }

}






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值