package org.lxh.demo;
import java.text.SimpleDateFormat;
import java.util.Date;
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 dialog, int whichButton) {
AlarmMessage.this.finish(); // 关闭对话框后本程序结束
}
}).show(); // 显示对话框
}
}
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 MyAlarmManagerDemo extends Activity {
private AlarmManager alarm = null; // 闹钟管理
private Button set = null; // 按钮组件
private Button delete = null; // 按钮组件
private TextView msg = null; // 文本显示组件
private Calendar calendar = Calendar.getInstance(); // 取得Calendar对象
private TimePicker time = null; // 时间选择器
private int hourOfDay = 0; // 保存设置的时
private int minute = 0; // 保存设置的分
@Override
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); // 24小时制
}
private class OnTimeChangedListenerImpl implements OnTimeChangedListener {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
MyAlarmManagerDemo.this.calendar.setTimeInMillis(System
.currentTimeMillis()); // 设置当前时间
MyAlarmManagerDemo.this.calendar.set(Calendar.HOUR_OF_DAY,
hourOfDay); // 设置小时
MyAlarmManagerDemo.this.calendar.
set(Calendar.MINUTE, minute); // 设置分钟
MyAlarmManagerDemo.this.calendar.
set(Calendar.SECOND, 0); // 设置秒
MyAlarmManagerDemo.this.calendar.
set(Calendar.MILLISECOND, 0); // 设置毫秒
MyAlarmManagerDemo.this.hourOfDay = hourOfDay; // 保存设置的小时
MyAlarmManagerDemo.this.minute = minute; // 保存设置的分钟
}
}
private class SetOnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
Intent intent = new Intent(MyAlarmManagerDemo.this,
MyAlarmReceiver.class); // 指定跳转的Intent
intent.setAction("org.lxh.action.setalarm"); // 定义广播的Action
PendingIntent sender = PendingIntent.getBroadcast(
MyAlarmManagerDemo.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT); // 指定PendingIntent
MyAlarmManagerDemo.this.alarm.set(AlarmManager.RTC_WAKEUP,
MyAlarmManagerDemo.this.calendar.getTimeInMillis(),
sender); // 设置闹钟
MyAlarmManagerDemo.this.msg.setText("闹钟响起的时间是:"
+ MyAlarmManagerDemo.this.hourOfDay + "时"
+ MyAlarmManagerDemo.this.minute + "分。"); // 提示文字
Toast.makeText(MyAlarmManagerDemo.this, "闹钟设置成功!",
Toast.LENGTH_SHORT).show(); // 显示提示信息
}
}
private class DeleteOnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
if (MyAlarmManagerDemo.this.alarm != null) {
Intent intent = new Intent(MyAlarmManagerDemo.this,
MyAlarmReceiver.class); // 设置Intent
PendingIntent sender = PendingIntent.getBroadcast(
MyAlarmManagerDemo.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT); // 指定PendingIntent
MyAlarmManagerDemo.this.alarm.cancel(sender); // 取消闹钟
MyAlarmManagerDemo.this.msg.setText("当前没有设置闹钟。"); // 提示文字
Toast.makeText(MyAlarmManagerDemo.this, "闹钟删除成功!",
Toast.LENGTH_SHORT).show(); // 显示提示信息
}
}
}
}
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
}
}
<?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"
android:gravity="center_horizontal">
<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>
<?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=".MyAlarmManagerDemo" 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=".AlarmMessage" />
<receiver
android:name="MyAlarmReceiver"
android:enabled="true"
android:process=":remote">
<intent-filter>
<action android:name="org.lxh.action.setalarm" />
</intent-filter>
</receiver>
</application>
</manifest>