1.定义闹钟的提示Activity程序类-AlarmManager
package org.lxh.demo;
import java.sql.Date;
import java.text.SimpleDateFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class AlarmMessage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new AlertDialog.Builder(this)//设置弹出窗
.setIcon(R.drawable.pic_m)
.setTitle("闹钟时间到!")
.setMessage(
"闹钟响起,现在时间是:"
+ new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒")
.format(new Date(System
.currentTimeMillis())))
.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
AlarmMessage.this.finish();
}
}).show();
}
}
2.定义广播接受类-MyAlarmReceiver
package org.lxh.demo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent it=new Intent(context,AlarmMessage.class);//定义要操作的Intent
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//传递一个新的任务标记
context.startActivity(it);//启动Intent
}
}
3.定义布局管理器-main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TimePicker
android:id="@+id/time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="当前没有设置闹钟"/>
<Button
android:id="@+id/set"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="设置闹钟" />
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="删除闹钟" />
</LinearLayout>
4.定义Activity操作闹钟
package org.lxh.demo;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;
public class Hello extends Activity {
private AlarmManager alarm = null;
private Button set = null;
private Button delete = null;
private TextView msg = null;
private Calendar calendar = Calendar.getInstance();
private TimePicker time = null;
private int hourOfDay = 0;
private int minute = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.main); // 设置要使用的布局管理器
this.time = (TimePicker) super.findViewById(R.id.time);
this.set = (Button) super.findViewById(R.id.set);
this.delete = (Button) super.findViewById(R.id.delete);
this.msg = (TextView) super.findViewById(R.id.msg);
this.set.setOnClickListener(new SetOnClickListenerImpl());
this.delete.setOnClickListener(new DeleteOnClickListenerImpl());
this.alarm = (AlarmManager) super
.getSystemService(Context.ALARM_SERVICE);
this.time.setOnTimeChangedListener(new OnTimeChangedListenerImpl());
this.time.setIs24HourView(true);
}
private class SetOnClickListenerImpl implements OnClickListener {
public void onClick(View arg0) {
Intent intent = new Intent(Hello.this, MyAlarmReceiver.class);//指定跳转的Intent
intent.setAction("org.lxh.action.setalarm");//定义广播的Action
PendingIntent sender = PendingIntent.getBroadcast(Hello.this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);//指定PendingIntent
Hello.this.alarm.set(AlarmManager.RTC_WAKEUP,
Hello.this.calendar.getTimeInMillis(), sender);//设置闹钟
Hello.this.msg.setText("闹钟响起的时间是:" + Hello.this.hourOfDay + "时"
+ Hello.this.minute + "分。");
Toast.makeText(Hello.this, "闹钟设置成功!", Toast.LENGTH_LONG).show();
}
}
private class DeleteOnClickListenerImpl implements OnClickListener {
public void onClick(View arg0) {
if(Hello.this.alarm!=null){
Intent intent = new Intent(Hello.this, MyAlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(Hello.this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
Hello.this.alarm.cancel(sender);//取消闹钟
Hello.this.msg.setText("当前没有设置闹钟。");
Toast.makeText(Hello.this, "闹钟删除成功!", Toast.LENGTH_LONG).show();
}
}
}
private class OnTimeChangedListenerImpl implements OnTimeChangedListener {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
Hello.this.calendar.setTimeInMillis(System.currentTimeMillis());//设置当前时间
Hello.this.calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);//设置时间
Hello.this.calendar.set(Calendar.MINUTE, minute);
Hello.this.calendar.set(Calendar.SECOND, 0);
Hello.this.calendar.set(Calendar.MILLISECOND, 0);
Hello.this.hourOfDay = hourOfDay;
Hello.this.minute = minute;
}
}
}
5.在AndroidManfiest.xml文件中进行配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lxh.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".Hello"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<span style="color:#ff0000;"><strong> <activity android:name=".AlarmMessage" />
<receiver
android:name="org.lxh.demo.MyAlarmReceiver"
android:enabled="true"
android:process=".remote" >
<intent-filter>
<action android:name="org.lxh.action.setalarm" />
</intent-filter>
</receiver></strong>
</span> </application>
</manifest>
运行实例