今天讲使用隐示intent创建一个闹钟的例子,如下:
Alarm Clock
Create an alarm
To create a new alarm, use the ACTION_SET_ALARM action and specify alarm details such as the time and message using extras defined below.
Note: Only the hour, minutes, and message extras are available in Android 2.3 (API level 9) and higher. The other extras were added in later versions of the platform.
Action
ACTION_SET_ALARM
Data URI
None
MIME Type
None
Extras
EXTRA_HOUR
The hour for the alarm.
EXTRA_MINUTES
The minutes for the alarm.
EXTRA_MESSAGE
A custom message to identify the alarm.
EXTRA_DAYS
An ArrayList including each week day on which this alarm should be repeated. Each day must be declared with an integer from the Calendar class such as MONDAY.
For a one-time alarm, do not specify this extra.
EXTRA_RINGTONE
A content: URI specifying a ringtone to use with the alarm, or VALUE_RINGTONE_SILENT for no ringtone.
To use the default ringtone, do not specify this extra.
EXTRA_VIBRATE
A boolean specifying whether to vibrate for this alarm.
EXTRA_SKIP_UI
A boolean specifying whether the responding app should skip its UI when setting the alarm. If true, the app should bypass any confirmation UI and simply set the specified alarm.
Example intent:
public void createAlarm(String message, int hour, int minutes) {
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
.putExtra(AlarmClock.EXTRA_MESSAGE, message)
.putExtra(AlarmClock.EXTRA_HOUR, hour)
.putExtra(AlarmClock.EXTRA_MINUTES, minutes);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
In order to invoke the ACTION_SET_ALARM intent, your app must have the SET_ALARM permission:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
Example intent filter:
<activity ...>
<intent-filter>
<action android:name="android.intent.action.SET_ALARM" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>