1. Action定义:
public static final String ALARM_SNOOZE_ACTION = "com.android.deskclock.ALARM_SNOOZE";
public static final String ALARM_DISMISS_ACTION = "com.android.deskclock.ALARM_DISMISS";
public static final String ALARM_DONE_ACTION = "com.android.deskclock.ALARM_DONE";
public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";
2.注册闹钟广播:
private void registerAlarm() {
IntentFilter filter = new IntentFilter();
filter.addAction("com.android.deskclock.ALARM_ALERT");
filter.addAction("com.android.deskclock.ALARM_DONE");
filter.addAction("com.android.deskclock.ALARM_DISMISS");
filter.addAction("com.android.deskclock.ALARM_SNOOZE");
registerReceiver(mReceiver, filter);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Tag", "AlarmActivity - Broadcast Receiver - " + action);
if (action.equals(ALARM_SNOOZE_ACTION)) {
// snooze();
} else if (action.equals(ALARM_DISMISS_ACTION)) {
// dismiss();
} else if (action.equals("com.android.deskclock.ALARM_ALERT")) {
new Thread(waitStopAlarm).start();
} else {
Log.i("Tag", "Unknown broadcast in AlarmActivity: " + action);
}
}
};
private Runnable waitStopAlarm = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO: handle exception
}
stopAlarm();
}
};
private void stopAlarm() {
Log.i("Tag", "stop alarm");
Intent intent = new Intent();
intent.setAction("com.android.deskclock.ALARM_DISMISS");
sendBroadcast(intent);
}
private void snoozeAlarm() {
Intent intent = new Intent();
intent.setAction("com.android.deskclock.ALARM_SNOOZE");
sendBroadcast(intent);
}
3.新增加闹钟:
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
intent.putExtra(AlarmClock.EXTRA_MESSAGE, "New Alarm!");
intent.putExtra(AlarmClock.EXTRA_HOUR, hour);
intent.putExtra(AlarmClock.EXTRA_MINUTES, minutes);
intent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
startActivity(intent);
4.权限:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
详细信息请移步:http://blog.csdn.net/yihongyuelan