For this project, you will be building a simple alarm system that allows the users to set a
date and time for the alarm to trigger. Once the specified date and time is reached, you
will display another activity containing some additional information.
First, modify the main.xml file as follows:
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSetAlarm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Alarm" />
</LinearLayout>
Add a new XML file to the res/layout folder and name it alarmdetails.xml. Populate it as
follows:
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Details of the alarm..." />
</LinearLayout>
This creates a very simple UI with a message as shown in the next figure:
Add a new Class file to the com.android.tienda package and name it AlarmDetails.java.
Populate it as follows:
package com.android.tienda;
import android.app.Activity;
import android.os.Bundle;
public class AlarmDetails extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.alarmdetails);
}
}
</activity>
<activity android:name=".AlarmDetails"
android:label="Details of the alarm">
<intent-filter>
<action android:name="com.android.tienda.AlarmDetails" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
So basically, the user will select the date and time for the alarm to go off. Once the alarm
goes off, it will display the AlarmDetails activity. The next section shows how to create the
alarm.
Creating an Alarm
To schedule an alarm in Android, you can make use of the AlarmManager class. The
AlarmManager class allows you to schedule an alarm to be triggered at a particular date
and time.
Add the following statements in bold to the MainActivity.java file:
package com.android.tienda;
import android.app.Activity;
import android.os.Bundle;
import
import
import
import
import
import
import
import
java.util.Calendar;
android.app.AlarmManager;
android.app.PendingIntent;
android.content.Intent;
android.view.View;
android.widget.Button;
android.widget.DatePicker;
android.widget.TimePicker;
The above code uses the Calendar class to obtain the current date and time. It then sets
the date and time specified by the user and uses this information to arm the
AlarmManager object. The PendingIntent object (displayIntent) is used to invoke the
AlarmDetails activity when the alarm is triggered.
To test the application, press F11 to deploy the application on the Android emulator. The
next figure shows the application running. Select a date and time that is later than the
current time (a suggestion is to set it to one minute later) and wait for the alarm to trigger.
android.app.Activity;
android.app.Notification;
android.app.NotificationManager;
android.app.PendingIntent;
android.content.Intent;
android.os.Bundle;
This activity will be called by the AlarmManager object when the alarm is triggered. Its
main use is to add a notification to the status bar through the use of the
NotificationManager class. Here, when a notification is added to the status bar, you will
also cause the device to vibrate, through the use of the vibrate property of the Notification
object. Note that every notification you create in your application has a unique identifier
so that later on you can use it to programmatically dismiss the notification. In this activity,
the identifier is passed in from the MainActivity activity. The PendingIntent object
(detailIntent) in this activity allows the user to invoke another activity when the
notification is tapped. In this case, tapping the notification in the status bar will invoke the
AlarmDetails activity. Finally, as this activity does not have an UI, you can simply call the
finish() method at the end of the onCreate() method to destroy the activity.
Modify the MainActivity.java as shown in bold below:
package com.android.tienda;
import android.app.Activity;
import android.os.Bundle;
import
import
import
import
import
import
import
import
java.util.Calendar;
android.app.AlarmManager;
android.app.PendingIntent;
android.content.Intent;
android.view.View;
android.widget.Button;
android.widget.DatePicker;
android.widget.TimePicker;
Here, you simply make the AlarmManager invoke the DisplayNotification activity when the
alarm is triggered. You use an ID of 1 to identify the notification. If you want to display
multiple notifications, you must use a unique ID for each notification.
Modify the AlarmDetails.java as follows:
package com.android.tienda;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
public class AlarmDetails extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.alarmdetails);
//---look up the notification manager service--NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
//---cancel the notification--nm.cancel(getIntent().getExtras().getInt("NotifID"));
The AlarmDetails activity will be displayed when the user taps on the notification on the
status bar. In this case, once the activity is displayed, you will dismiss the notification
using the cancel() method of the NotificationManager object. This method takes in the ID
of the notification, which you have passed in earlier through the MainActivity and
DisplayNotification activities.
Add the following code to the AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.tienda"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name="MainActivity"
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=".AlarmDetails"
android:label="Details of the alarm">
<intent-filter>
<action android:name="com.android.tienda.AlarmDetails" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".DisplayNotifications" >
<intent-filter>
<action android:name="com.android.tienda.DisplayNotifications" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Finally, you can now test the application. Press F11 in Eclipse to test the application on the
Android emulator. When the application is loaded, select a time and click the Set Alarm
button. Once this is done, click the Back button to dismiss the activity. When the alarm is
triggered, you will see the notification on the status bar (see the next figure).
Clicking and dragging the status bar down will reveal the details of the notifications (see
the Next figure).
Summary
In this article, you have seen how to display a status notification in your Android
application. Through this example, you have also got the chance to see how to use the
AlarmManager class to set an alarm in your application.
Fuente
http://mobiforge.com/developing/story/displaying-status-bar-notifications-android