Android notification example (1)

Source: Internet
Author: User

Such a scenario: Reimbursement approval in OA. If the boss is busy with official business and often goes on a business trip, employees often complain that they cannot get timely reimbursement approval. Therefore, similar approval requirements are frequently used in mobile OA.

The following example shows how to use Android notifications.

Write a simple Activity and press the button to generate a notification with a sound prompt.

Drop-down notification bar:

 

Click the notification entry and re-enter the simple Activity above. Of course, other activities can be opened. This is a simple example.

You can see that the notification icon displayed this time disappears after you click it.

For the source code, see:

http://easymorse.googlecode.com/svn/trunk/android.notification/

The code that mainly works is the processing part after clicking the button:

1 button. setOnClickListener (new OnClickListener (){
2 @ Override
3 public void onClick (View v ){
4 icationicationmanager manager = (NotificationManager) getSystemService (Context. icationication_service );
5 Notification notification = new Notification (R. drawable. icon,
6 "received documents", System. currentTimeMillis ());
7 notification. setLatestEventInfo (getApplicationContext (), "Zhang San-reimbursement approval ",
8 "Michael Zhang-travel expenses-Shanghai World Expo-3646 yuan", PendingIntent. getActivity (
9 LaunchNotificationActivity. this, 0,
10 new Intent (LaunchNotificationActivity. this, LaunchNotificationActivity. class), 0 ));
11 notification. flags | = Notification. FLAG_AUTO_CANCEL;
12 notification. defaults | = Notification. DEFAULT_SOUND;
13 manager. Y (1, notification );
14}
15 });
 

The icationicationmanager is a system service that already exists and can be obtained through the getSystemService () method.

Some parameters, icons, text for prompts, and Notification time are required during Notification creation:

1 Notification notification = new Notification (R. drawable. icon,
2 "received documents", System. currentTimeMillis ());

You also need to set some content in the drop-down notification bar: Question, text, and click the action of the notification entry. In this example, click and jump to an Activity:

1 notification. setLatestEventInfo (getApplicationContext (), "Zhang San-reimbursement approval ",
2 "Zhang San-travel expenses-Shanghai World Expo-3646 yuan", PendingIntent. getActivity (
3 LaunchNotificationActivity. this, 0,
4 new Intent (LaunchNotificationActivity. this, LaunchNotificationActivity. class), 0 ));
1. After clicking a button, it is no longer displayed in the notification bar:
2
3 notification. flags | = Notification. FLAG_AUTO_CANCEL;
4
5 set the sound:
6
7 notification. defaults | = Notification. DEFAULT_SOUND;
8
9 Add the notification to NotifacationManager:
10
11 manager. Y (1, notification );
12
13 The number here is an id, and here it is constant 1. If you click the "Receive document" button multiple times, no notifications will appear. If you add 1 at a time, multiple notifications can be generated.

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.