Android Program Development: (4) send Notification-4.2 Notification

Source: Internet
Author: User

So far, everyone must be familiar with using Toast to display information to users. Although Toast is easy to use, the notifications displayed by Toast are not permanently stored. It only shows a short period of time on the screen, and then disappears. If it contains important information, if the user does not observe the screen, the user will easily miss it.

A more persistent storage method should be used for important information. In this case, icationicationmnanger (Message manager) should be used to display a persistent message, which is displayed on the StatusBar (status bar) and can be easily seen by users.

Next, we will show you how to send a Notification.

1. Create a project named Notifications.

2. Create a class named icationicationview in the package, and create a file named notification. xml under the res/layout folder, which will serve as the view of icationicationview.

3. Files in notification. xml.
 
[Html] view plaincopy
<? 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 = "Here are the details for the notification..."/>
 
</LinearLayout>
 
4. icationicationview. java code.
 
Package net. learn2develop. configurications;
 
Import android. app. Activity;
Import android. app. icationicationmanager;
Import android. OS. Bundle;
 
Public class icationicationview extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. notification );
 
// --- Look up the notification manager service ---
Icationicationmanager nm = (NotificationManager) getSystemService (NOTIFICATION_SERVICE );
 
// --- Cancel the notification that we started ---
Nm. cancel (getIntent (). getExtras (). getInt ("icationicationid "));
}
}
5. The code in AndroidManifest. xml.
 
[Html] view plaincopy
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "net. learn2develop. configurications"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
 
<Uses-sdk android: minSdkVersion = "14"/>
<Uses-permission android: name = "android. permission. VIBRATE"/>
 
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Activity
Android: label = "@ string/app_name"
Android: name = ". NotificationsActivity">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Activity android: name = ". NotificationView"
Android: label = "Details of notification">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
</Activity>
</Application>
 
</Manifest>
6. Code in main. xml.
[Html] view plaincopy
<? 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">
 
<Button
Android: id = "@ + id/btn_displaynotif"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Display Notification"
Android: onClick = "onClick"/>
 
</LinearLayout>
7. Finally, the Code in icationicationactivity. java.
 
Package net. learn2develop. configurications;
 
Import android. app. Activity;
Import android. app. Notification;
Import android. app. icationicationmanager;
Import android. app. PendingIntent;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
 
Public class NotificationsActivity extends Activity {
Int icationicationid = 1;

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}

Public void onClick (View view ){
DisplayNotification ();
}

Protected void displayNotification ()
{
// --- PendingIntent to launch activity if the user selects
// This notification ---
Intent I = new Intent (this, icationicationview. class );
I. putExtra ("icationicationid", notificationID );
 
PendingIntent pendingIntent =
PendingIntent. getActivity (this, 0, I, 0 );
 
Icationicationmanager nm = (NotificationManager)
GetSystemService (icationication_service );
 
Notification notif = new Notification (
R. drawable. ic_launcher,
"Reminder: Meeting starts in 5 minutes ",
System. currentTimeMillis (); www.2cto.com
 
CharSequence from = "System Alarm ";
CharSequence message = "Meeting with customer at 3 ...";

Notif. setLatestEventInfo (this, from, message, pendingIntent );
 
// --- 100 ms delay, vibrate for 250 ms, pause for 100 ms and
// Then vibrate for 500 ms ---
Notif. vibrate = new long [] {100,250,100,500 };
Nm. notify (icationicationid, notif );
}
 
}
8. debugging. 9. Click the Display Notification button, and a notification Notification will appear on the status bar.

 

10. Pull down the status bar to display detailed information about the Notification.

11. Click this Notification to display the NotificationView interface. At the same time, the Notification on the status bar disappears.


From manoel's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.